目录
多态参数
父类-Employee类
子类-Worker类
子类-Manager类
Test类-要求1
main类-PolyParameter
在main类中调用Test类的showEmpAnnual(Employee e) 方法
运行结果
Test类-要求2
代码
main方法内调用
分析
运行结果
多态参数
方法定义的形参类型是父类,实参类型可以是子类

父类-Employee类
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;
    }
}
子类-Worker类
package com.hspedu.poly_.polyparameter;
public class Worker extends Employee{
    public Worker(String name, double monthsal) {
        super(name, monthsal);
    }
    //重写
    @Override
    public double getAnnual() {
        //普通员工没有其他收入
        return super.getAnnual();
    }
    //work方法
    public void  work(){
        System.out.println("员工" + getName() + "正在工作");
    }
}
子类-Manager类
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;
    }
    //重写
    @Override
    public double getAnnual() {
        //管理者是工资+奖金
        return super.getAnnual() + bonus;
    }
    public void manage(){
        System.out.println("经理" + getName() + "负责财务方面的管理");
    }
}
Test类-要求1
测试类中添加一个方法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类-PolyParameter
在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);
        
    }
}
在main类中调用Test类的showEmpAnnual(Employee e) 方法
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);
      
    }
}
运行结果

Test类-要求2
测试类中添加一个方法,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();
        }
    }
}main方法内调用
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;也就是让父类的引用类型指向子类的对象,即向上转型,所以在方法内需要先向下转型,然后调用
运行结果




















