目录
多态参数
父类-Employee类
子类-Worker类
子类-Manager类
Test类-要求1
main类-PolyParameter
在main类中调用Test类的showEmpAnnual(Employee e) 方法
运行结果
Test类-要求2
代码
main方法内调用
分析
运行结果
方法定义的形参类型是父类,实参类型可以是子类
package com.hspedu.poly_.polyparameter;public class Employee {private String name;private double monthsal;public Employee(String name, double monthsal) {this.name = name;this.monthsal = monthsal;}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getMonthsal() {return monthsal;}public void setMonthsal(double monthsal) {this.monthsal = monthsal;}//计算年工资public double getAnnual(){return monthsal * 12;}
}
package com.hspedu.poly_.polyparameter;public class Worker extends Employee{public Worker(String name, double monthsal) {super(name, monthsal);}//重写@Overridepublic double getAnnual() {//普通员工没有其他收入return super.getAnnual();}//work方法public void work(){System.out.println("员工" + getName() + "正在工作");}
}
package com.hspedu.poly_.polyparameter;public class Manager extends Employee{private double bonus;public Manager(String name, double monthsal, double bonus) {super(name, monthsal);this.bonus = bonus;}public double getBonus() {return bonus;}public void setBonus(double bonus) {this.bonus = bonus;}//重写@Overridepublic double getAnnual() {//管理者是工资+奖金return super.getAnnual() + bonus;}public void manage(){System.out.println("经理" + getName() + "负责财务方面的管理");}
}
测试类中添加一个方法showEmpAnnual(Employee e) ,实现获取任何员工对象的年工资并在main方法中调用该方法[e.getAnnual()]
package com.hspedu.poly_.polyparameter;public class Test {//测试类中添加一个方法showEmpAnnual(Employee e)//实现获取任何员工对象的年工资并在main方法中调用该方法[e.getAnnual()]public void showEmpAnnual(Employee e) {System.out.println(e.getAnnual());}
}
在main类中创建Woker类和Manager类的新对象,并完成对象属性的初始化
package com.hspedu.poly_.polyparameter;public class PolyParameter {public static void main(String[] args) {Worker bobby = new Worker("Bobby", 5000);Manager carol = new Manager("Carol", 12000, 80000);}
}
package com.hspedu.poly_.polyparameter;public class PolyParameter {public static void main(String[] args) {Worker bobby = new Worker("Bobby", 5000);Manager carol = new Manager("Carol", 12000, 80000);//PolyParameter polyParameter = new PolyParameter();Test test = new Test();//员工工资test.showEmpAnnual(bobby);test.showEmpAnnual(carol);}
}
测试类中添加一个方法,testWork,如果是普通员工, 则调用work方法,如果是经理,则调用manage方法
package com.hspedu.poly_.polyparameter;public class Test {//测试类中添加一个方法showEmpAnnual(Employee e)//实现获取任何员工对象的年工资并在main方法中调用该方法[e.getAnnual()]public void showEmpAnnual(Employee e) {System.out.println(e.getAnnual());}//测试类中添加一个方法,testWork,如果是普通员工,//则调用work方法,如果是经理,则调用manage方法public void testWork(Employee e) {if (e instanceof Worker) {//向下转型,然后调用
// Worker worker = (Worker) e;
// worker.work();//合并上述两句话((Worker) e).work();} else if (e instanceof Manager) {((Manager) e).manage();}}
}
package com.hspedu.poly_.polyparameter;public class PolyParameter {public static void main(String[] args) {Worker bobby = new Worker("Bobby", 5000);Manager carol = new Manager("Carol", 12000, 80000);//PolyParameter polyParameter = new PolyParameter();Test test = new Test();//员工工资test.showEmpAnnual(bobby);test.showEmpAnnual(carol);//调用子类特有的方法//bobby的运行类型是//在传参的时候把bobby传给e,就是Employee e=bobby,// 完成了一个向上转型,所以在调用的时候还要一次向下转型test.testWork(bobby);test.testWork(carol);}
}
方法testWork的形参是(Employee e),实参是传入的bobby或者是carol,执行传参的时候程序执行
Employee e = bobby;也就是让父类的引用类型指向子类的对象,即向上转型,所以在方法内需要先向下转型,然后调用