Employee e = null;
if(tokens[0].equals("E")) {
e = new SalaryEmployee(id, firstName, lastName, title, annualSalary);
} else if(tokens[0].equals("S")) {
e = new Staff(id, firstName, lastName, title, hourlyPayRate, hoursWorked); 、
} else if(tokens[0].equals("T")) {
e = new Temporary(id, firstName, lastName, title, hourlyPayRate, hoursWorked);
}
其中父类是Employee,我用new实例化的对象都是子类,我就想问一下在这种情况下怎么调用子类的方法?
谢谢!这里是要用到子类方法的地方。红色的都是子类里的方法。
for(Employee e : payroll) {
//format their information
sb.append(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()));
}
|