package test;
class Employee{
public String getSalary(){
return "基本工资";
}
}
class Manager extends Employee{
public String getSalary(){
return "基本工资、"+"奖金";
}
}
class CEO extends Employee{
public String getSalary(){
return "基本工资、"+"奖金、"+"分红";
}
}
public class Test3 {
public static void main(String[] args) {
Employee[] employee = new Employee[3];
employee[0] = new Employee();
employee[1] = new Manager();
employee[2] = new CEO();
//一个的对象变量(例如,变量e)可以表示多种实际类型的现象称为多态;
//e可以表示Employee \Manager \CEO 的对象( 多态)
for (Employee e : employee) {
//在运行时能够自动的选择调用哪个方法的现象称为动态绑定
//首先调用自己的getSalary()方法,自己未定义该方法则调用父类的同名方法
System.out.println(e.getSalary());
}
}
}