1.方法定义的参数类型为父类类型,实参类型允许为子类类型
public class Ploy_parameter {public static void main(String[] args) {Manage jack = new Manage("jack",12000,3000);Staff tom = new Staff("tom",10000);Ploy_parameter ploy_parameter = new Ploy_parameter();ploy_parameter.showEmpAninal(jack);ploy_parameter.showEmpAninal(tom);ploy_parameter.testWork(jack);ploy_parameter.testWork(tom);}public void showEmpAninal(Employee e){System.out.println(e.getAnnual());}public void testWork(Employee e){if(e instanceof Staff){((Staff) e).work();}else if(e instanceof Manage){((Manage) e).manage();}else{System.out.println("员工不存在");}}
}
class Employee{private String name;private double salary;public Employee(String name, double salary) {this.name = name;this.salary = salary;}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getSalary() {return salary;}public void setSalary(double salary) {this.salary = salary;}public double getAnnual(){return salary * 12;}
}
class Manage extends Employee{private double bonus;public Manage(String name, double salary, double bonus) {super(name, salary);this.bonus = bonus;}public double getBonus() {return bonus;}public void setBonus(double bonus) {this.bonus = bonus;}public void manage(){System.out.println("经理" + getName() + "正在管理中");}public double getAnnual(){return super.getAnnual() + bonus;}
}
class Staff extends Employee{public Staff(String name, double salary) {super(name, salary);}public void work(){System.out.println("员工" + super.getName() + "正在工作中");}public double getAnnual(){return super.getAnnual();}
}