当前位置 : 主页 > 编程语言 > c语言 >

多态的应用

来源:互联网 收集:自由互联 发布时间:2023-09-06
1. 多态数组 数组的定义类型为父类类型,里面保存的实际元素类型为子类类型 1.1 应用实例: 315 现有一个继承结构如下:要求创建 1 个 Person 对象、2 个 Student 对象和 2 个 Teacher 对象, 统

1. 多态数组

数组的定义类型为父类类型,里面保存的实际元素类型为子类类型

1.1 应用实例: 315

现有一个继承结构如下:要求创建 1 个 Person 对象、2 个 Student 对象和 2 个 Teacher 对象, 统一放在数组 中,并调用每个对象say 方法.

代码在com.stulzl.poly_array.包中

测试PloyArray
package com.stulzl.poly_array;

//多态数组  315
//现有一个继承结构如下:要求创建 1 个 Person 对象、2 个 Student 对象和 2 个 Teacher 对象,
// 统一放在数组 中,并调用每个对象say 方法.

public class PloyArray {
    public static void main(String[] args) {

        Person persons[] = new Person[5];//多态数组
        persons[0]=new Person("jack",20);
        persons[1]=new Student("jack",18,100);
        persons[2]=new Student("smith",19,30);
        persons[3]=new Teacher("july",30,20000);
        persons[4]=new Teacher("king",50,25000);

        //循环遍历数组
        for (int i = 0; i < persons.length; i++) {
            //person[i] 编译类型是 Person ,运行类型是是根据实际情况有 JVM 来判断
            System.out.println(persons[i].say());//有动态绑定机制
        }
    }
}

父类Person

package com.stulzl.poly_array;

public class Person {//父类
    private String name;
    private  int age;
    //构造器
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    //返回信息
    public String say(){
        return name+"\t"+age;
    }
}

子类Student

package com.stulzl.poly_array;

public class Student extends Person{//子类
    private double score;

    public Student(String name, int age, double score) {
        super(name, age);
        this.score = score;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    //返回信息
    public String say(){
        return "学生 "+super.say()+" "+score;
    }
}

子类Teacher

package com.stulzl.poly_array;

public class Teacher extends Person{//子类
    private  double salary;

    public Teacher(String name, int age, double salary) {
        super(name, age);
        this.salary = salary;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
    //返回信息
    public String say(){
        return "老师 "+super.say()+" "+salary;
    }
}

1.2应用实例升级:316

如何调用子类特有的方法,比如 Teacher 有一个 teach , Student 有一个 study 怎么调用?

代码在com.stulzl.poly_array_pro.包中

测试PloyArray
package com.stulzl.poly_array_pro;

public class PolyArray {
    public static void main(String[] args) {
        Person persons[] = new Person[5];//多态数组
        persons[0]=new Person("jack",20);
        persons[1]=new Student("marry",18,100);
        persons[2]=new Student("smith",19,30);
        persons[3]=new Teacher("july",30,20000);
        persons[4]=new Teacher("king",50,25000);

        //循环遍历数组
        for (int i = 0; i < persons.length; i++) {
            //person[i] 编译类型是 Person ,运行类型是是根据实际情况有 JVM 来判断
            System.out.println(persons[i].say());//有动态绑定机制

            if(persons[i] instanceof Student){//判断person[i]的运行类型是不是Student
                Student student = (Student)persons[1];//向下转型
                student.study();
            }else if(persons[i] instanceof Teacher){
                Teacher teacher = (Teacher)persons[i];
                teacher.teach();
            }else if(persons[i] instanceof Person) {

            }else{
                System.out.println("您的运行类型有误");
            }
        }

    }
}
父类Person
package com.stulzl.poly_array_pro;

public class Person {//父类
    private String name;
    private int age;

    //构造器
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    //返回信息
    public String say() {
        return name + "\t" + age;
    }
}
子类Student
package com.stulzl.poly_array_pro;


public class Student extends Person {//子类
    private double score;

    public Student(String name, int age, double score) {
        super(name, age);
        this.score = score;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    //返回信息
    public String say(){
        return "学生 "+super.say()+" "+score;
    }
    //特有方法
    public void study(){
        System.out.println("学生 "+getName()+" 正在学java课程");
    }
}
子类Teacher
package com.stulzl.poly_array_pro;


public class Teacher extends Person {//子类
    private  double salary;

    public Teacher(String name, int age, double salary) {
        super(name, age);
        this.salary = salary;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
    //返回信息
    public String say(){
        return "老师 "+super.say()+" "+salary;
    }
    //特有方法
    public void teach(){
        System.out.println("老师 "+getName()+" 正在讲java课程");
    }
}

2. 多态参数 317

方法定义的形参类型为父类类型,实参类型允许为子类类型

2.1 例 317

多态的应用_System

代码在com.stulzl.poly_parameter.包中

测试PolyParameter

package com.stulzl.poly_parameter;

//多态参数  317
public class PolyParameter {
    public static void main(String[] args) {
        Worker tom = new Worker("tom",2500);
        Manager milan = new Manager("milan",5000,200000);
        //这里是声明对象调用本类方法
        PolyParameter polyParameter = new PolyParameter();
        polyParameter.showEmpAnnual(tom);
        polyParameter.testWork(tom);
        polyParameter.showEmpAnnual(milan);
        polyParameter.testWork(milan);
    }
    //showEmpAnnual(Employee e)
    //实现获取任何员工对象的年工资,并在 main 方法中调用该方法 [e.getAnnual()]
    public void showEmpAnnual(Employee e){//使用父类接收,编译类型父类Employee,运行类型具体看子类
        System.out.println(e.getAnnual());//这里涉及动态绑定机制

    }
    //添加一个方法,testWork,如果是普通员工,则调用 work 方法,如果是经理,则调用 manage 方法
    public void testWork(Employee e){//还是编译类型父类Employee,运行类型具体看子类
        if(e instanceof Worker){//判断e的运行类型是不是Worker的类型
            Worker worker = (Worker)e;//向下强转
            worker.work();
        }else if(e instanceof Manager){
            Manager manager = (Manager)e;
            manager.manager();
        }else{
            System.out.println("不做处理");
        }
    }
}

父类Employee

package com.stulzl.poly_parameter;

public 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 12*salary;
    }
}

子类Worker

package com.stulzl.poly_parameter;

public class Worker extends Employee {//子类
    public Worker(String name, double salary) {
        super(name, salary);
    }
    //特有的工作方法
    public void work(){
        System.out.println("普通员工 "+getName()+" 在工作");
    }
    //重写获取年薪的方法,因为普通员工没有其他收入,则直接调用父类方法getAnnual()
    public double getAnnual() {
        return super.getAnnual();
    }
}

子类Manager

package com.stulzl.poly_parameter;

public class Manager extends Employee {//子类
    private double bonus;

    public Manager(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 manager(){
        System.out.println("经理 "+getName()+" 正在管理");
    }
    //重写获取年薪的方法
    public double getAnnual(){
        return super.getAnnual()+bonus;
    }
}


上一篇:经典八大排序
下一篇:没有了
网友评论