Java学习之多态数组

目录

一、定义

二、举例说明

要求1

父类-Person

子类-Student

子类-Teacher

main类

运行结果

 要求2

 思路分析

main类中的代码

运行结果


一、定义

数组的定义类型为父类类型, 里面保存的实际元素类型为子类类型(也可以有父类)

二、举例说明

要求1

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

先按照要求创建父类和子类

父类-Person

package com.hspedu.poly_.polyarr_;

public class Person {
    int n1 = 200;
    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;
    }
    //say()方法
    public String say() {
        return name + "\t" + age;
    }
}


子类-Student

package com.hspedu.poly_.polyarr_;

public class Student extends Person{
    int n1 = 100;
    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;
    }
    //重写父类say()方法

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

}

子类-Teacher

package com.hspedu.poly_.polyarr_;

public class Teacher extends Person{
    int n1 = 300;
    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;
    }
}

main类

在main类中创建多态数组,创建一个Person类的一维数组,保存的元素为Person类和其子类(运行类型)的对象

package com.hspedu.poly_.polyarr_;

public class PolyArr {
    public static void main(String[] args) {
        //新建一个int型一维数组有5个元素
        //int[] arr = new int[5];
        //新建一个Person型一维数组,有5个元素,即5个Person类的对象
        Person[] person = new Person[5];
        person[0] = new Person("jack", 23);
        //向上转型后,有两个Student对象和两个Teacher对象
        person[1] = new Student("bobby", 25, 65.5);
        person[2] = new Student("smith", 22, 80.6);
        person[3] = new Teacher("scott", 45, 14500);
        person[4] = new Teacher("james", 36, 8432.6);
        for (int i = 0; i < person.length; i++) {
            //动态绑定机制,会根据运行类型去查找方法
            //调用person[1].say()时,person[1]的运行类型是Student
            //会从Student类开始查找调用say()方法
            System.out.println(person[i].say());
        }

    }
}

运行结果

 要求2

如何调用子类特有的方法, 比如Teacher 有一个 teach , Student 有一个 study怎么调用?
在Student类和Teacher类中创建子类独有的方法

 

 思路分析

根据前面所学的知识,在向上转型后如果要调用子类独有的方法必须要将person[i]向下转型,

因为上述的person数组中的元素的运行类型有三种:Person、Student、Teacher,所以要先判断对象的运行类型,此时需要用到前面所学的instanceof(判断对象的运行类型是否为 XX 类型或 XX 类型的子类型)

main类中的代码

package com.hspedu.poly_.polyarr_;

public class PolyArr02 {
        public static void main(String[] args) {
            //新建一个int型一维数组有5个元素
            //int[] arr = new int[5];
            //新建一个Person型一维数组,有5个元素,即5个Person类的对象
            Person[] person = new Person[5];
            person[0] = new Person("jack", 23);
            //向上转型
            person[1] = new Student("bobby", 25, 65.5);
            person[2] = new Student("smith", 22, 80.6);
            person[3] = new Teacher("scott", 45, 14500);
            person[4] = new Teacher("james", 36, 8432.6);
            for (int i = 0; i < person.length; i++) {
                //动态绑定机制,会根据运行类型去查找方法
                //调用person[1].say()时,person[1]的运行类型是Student
                //会从Student类开始查找调用say()方法
                System.out.println(person[i].say());

                //    如何调用子类特有的方法, 比如Teacher 有一个 teach
                //    Student 有一个 study, 怎么调用?
                //    调用子类独有的方法,需要向下转型

                //先判断运行类型是不是Student类
                if (person[i] instanceof Student) {
                    /*
                    先向下转型,然后调用
                    Student student = (Student)person[i];
                    student.study();
                     */
                    //将以上两步合并成一步
                    ((Student) person[i]).study();
                } else if (person[i] instanceof Teacher) {
                    //原理同上
                    ((Teacher) person[i]).teach();
                } else if (person[i] instanceof Person) {

                } else {
                    System.out.println("你输入的类型有误,请重新检查");
                }

            }
        }
}

先判断,然后向下转型,然后调用
                    Student student = (Student)person[i];
                    student.study();

将上面两句合并
      ((Student) person[i]).study();             

运行结果

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值