目录
一、定义
二、举例说明
要求1
父类-Person
子类-Student
子类-Teacher
main类
运行结果
要求2
思路分析
main类中的代码
运行结果
数组的定义类型为父类类型, 里面保存的实际元素类型为子类类型(也可以有父类)
现有一个继承结构如下: 要求创建 1 个 Person 对象、 2 个 Student 对象和 2 个 Teacher 对象, 统一放在数组中, 并调用每个对象say 方法.
先按照要求创建父类和子类
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;}
}
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()方法@Overridepublic String say() {return "学生" + super.say() + " score=" + score;}}
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;}@Overridepublic String say() {return "老师" + super.say() + " salary=" + salary;}
}
在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());}}
}
如何调用子类特有的方法, 比如Teacher 有一个 teach , Student 有一个 study怎么调用?
在Student类和Teacher类中创建子类独有的方法
根据前面所学的知识,在向上转型后如果要调用子类独有的方法必须要将person[i]向下转型,
因为上述的person数组中的元素的运行类型有三种:Person、Student、Teacher,所以要先判断对象的运行类型,此时需要用到前面所学的instanceof(判断对象的运行类型是否为 XX 类型或 XX 类型的子类型)
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();