【JavaSE】多态数组的使用

简介: 【JavaSE】多态数组的使用

1. 多态数组

  • 数组的定义类型为父类类型,里面保存的实际元素类型为子类类型
  • 应用实例:现有一个继承结构如下:要求创建 1 个 Person 对象、2 个 Student 对象和 2 个 Teacher 对象, 统一放在数组中,并调用每个对象say方法。

在这里插入图片描述

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;
    }
}
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;
    }
    //重写父类say方法
    @Override
    public String say() {
        return super.say() + " score=" + score;
    }
}
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;
    }
    //重写父类 say 方法
    @Override
    public String say() {
        return super.say() + " salary=" + salary;
    }
}
public class PloyArray {
    public static void main(String[] args) {
        Person[] person = new Person[5];
        person[0] = new Person("jack", 20);
        person[1] = new Student("mike", 21, 90);
        person[2] = new Student("smith", 18, 100);
        person[3] = new Teacher("scott", 30, 20000);
        person[4] = new Teacher("king", 50, 30000);

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

在这里插入图片描述

  • 应用实例升级:如何调用子类特有的方法,比如:Teacher 有一个 teach()方法, Student 有一个 study() 方法,怎么调用?
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;
    }
}
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;
    }
    //重写父类say方法
    @Override
    public String say() {
        return super.say() + " score=" + score;
    }
    //特有方法
    public void study(){
        System.out.println("学生 " + getName() + " 正在学Java课程");
    }
}
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;
    }
    //重写父类 say 方法
    @Override
    public String say() {
        return super.say() + " salary=" + salary;
    }
    //特有方法
    public void teach(){
        System.out.println("老师 " + getName() + " 正在讲Java课程");
    }
}
public class PloyArray {
    public static void main(String[] args) {
        Person[] person = new Person[5];
        person[0] = new Person("jack", 20);
        person[1] = new Student("mike", 21, 90);
        person[2] = new Student("smith", 18, 100);
        person[3] = new Teacher("scott", 30, 20000);
        person[4] = new Teacher("king", 50, 30000);

        //循环遍历多态数组,调用 say() 方法
        for (int i = 0; i < person.length; i++) {
            //提示:Person[i]编译类型是 person,运行类型是根据实际情况JVM来判断
            System.out.println(person[i].say()); //动态绑定机制
            //使用类型判断 + 向下转型
            if (person[i] instanceof Student){//判断 person[i]运行类型是不是Student
                Student student = (Student) person[i];//向下转型
                student.study();
                //也可以使用  (Student) person[i].study();
            }else if (person[i] instanceof Teacher) {
                Teacher teacher = (Teacher) person[i];
                teacher.teach();

            }else if (person[i] instanceof Person){

            }else{
                System.out.println("你的类型有误,请自行检查");
            }
        }


    }
}

在这里插入图片描述

目录
相关文章
|
Java 编译器
【JAVASE】类与对象 下
【JAVASE】类与对象
|
存储 Java 编译器
【javaSE】 类和对象详解(下)
【javaSE】 类和对象详解(下)
|
8月前
javaSE&多态
javaSE&多态
40 1
|
安全 Java
【JAVASE】多态 下
【JAVASE】多态
|
8月前
|
存储 SQL Java
【JavaSE语法】类和对象(二)
本文主要介绍了面向对象的三大特点之一封装,并引入了包的概念;还介绍了static修饰类的成员(变量+方法),最突出的特点就是static修饰的属于类,而不属于某个对象;最后介绍了四种代码块
55 7
|
安全 Java 程序员
JavaSE继承和多态
JavaSE继承和多态
|
存储 Java 编译器
【JavaSE语法】类和对象(一)
【JavaSE语法】类和对象(一)
54 0
|
存储 安全 Java
类和对象【JavaSE】
类和对象【JavaSE】
86 0
|
Java Android开发
【javaSE】 类和对象详解(上)
【javaSE】 类和对象详解(上)
|
Java 编译器
【JAVASE】多态 上
【JAVASE】多态