多态数组

多态数组的应用1

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

继承结构如下:

创建1个Person对象,2个Student对象和2个Teacher对象,统一放在数组中,并调用say方法

父类Person:

package hspedu.poly_.polyarr_;

public class Person {
    private String name;
    private int age;

    public Person() {
    }

    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 hspedu.poly_.polyarr_;

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;
    }

    @Override
    public String say() {
        return super.say() + " score=" + score;
    }
}

子类Teacher:

package hspedu.poly_.polyarr_;

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;
    }

    @Override
    public String say() {
        return super.say() + " salary=" + salary;
    }
}

测试类PolyArray:

package hspedu.poly_.polyarr_;

public class PolyArray {
    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.1);
        persons[3] = new Teacher("scott",30,20000);
        persons[4] = new Teacher("king",50,25000);

        //循环遍历多态数组,调用say()
        for (int i = 0;i < persons.length;i++){
            System.out.println(persons[i].say());
        }
    }
}

jack 20
jack 18 score=100.0
smith 19 score=30.1
scott 30 salary=20000.0
king 50 salary=25000.0

Process finished with exit code 0

多态数组的应用2

在for 循环中调用子类的特有方法:

package hspedu.poly_.polyarr_;

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.1);
        persons[3] = new Teacher("scott",30,20000);
        persons[4] = new Teacher("king",50,25000);

        //循环遍历多态数组,调用say()
        for (int i = 0;i < persons.length;i++){
            System.out.println(persons[i].say());
            //persons[i].study();	//编译类型是Person,找不到子类特有方法,无法通过编译
            //persons[i].teach();
            if (persons[i] instanceof Student){ //判断person[i] 的运行类型是不是Student
                Student student = (Student) persons[i];	//强转引用的类型为子类类型,并将引用复制给另一个新的子类类型引用
                student.study();	//使用新的引用去调用子类特有方法
            }else if (persons[i] instanceof Teacher){//判断person[i] 的运行类型是不是Teacher
                Teacher teacher = (Teacher) persons[i];
                teacher.teach();
            }else if (persons[i] instanceof Person){
//                System.out.println("你的类型有误,请自行检查!");
            }else
                System.out.println("你的类型有误,请自行检查!");
        }
    }
}
posted @ 2023-06-07 10:26  Hello_world_名字被取了  Views(14)  Comments(0Edit  收藏  举报