62,623
社区成员
发帖
与我相关
我的任务
分享
public abstract class Employee {
...
public abstract String getDescription();
}
这个方法在不同的子类中有不同的实现,比如 Staff:
public class Staff extends Employee {
...
@Override
public String getDescription() {
return String.format("%-8s %-20s %-10s %-30s $%8.2f $%8.2f $%8.2f\n",
e.getID(), e.getName(), e.getType(), e.getTitle(), e.getGross(), e.getTax(), e.getNetpay());
}
}
而在你的循环中,你只要调用 Employee 的 getDescription() 就可以了。这是普遍使用的解决办法。