多态数组
- 多态数组:数组的定义类型为父类类型,里面保存的实际元素类型为子类类型
应用实例-1
现有一继承结构,要求创建一个person对象、两个student对象和两个teacher对象,统一放在数组中,并调用每个对象的say方法。
![image-20240228172630018](https://img-blog.csdnimg.cn/img_convert/16ee14ca4fa20aedd4c4a197a475bbb3.png)
PloyArray类
public class PloyArray{
public static void main(String[] args){
Person[] person = new Person[5];
person[0] = new Person("zhangsan",30);
person[1] = new Student("wangwu",18,74.0);
person[2] = new Student("wangpeng",17,89.5);
person[3] = new Teacher("zhangxiwang",32,15000);
person[4] = new Teacher("luoping",32,20000);
//遍历多态数组调用say方法
for (int i = 0; i < person.length; i++) {
System.out.println(person[i].say()); //动态绑定机制
}
}
}
Person类
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;
}
}
Teacher类
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(){
this.salary = salary;
}
@Override
public String say() {
return super.say() + " salary=" + salary;
}
}
Student类
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方法
public String say(){
return super.say() + " score=" + score;
}
}
运行效果
应用实例-2
如何调用子类特有的方法,例如:teacher类有一个teach方法,student类有一个study方法,该如何调用呢
PloyArray类
public class PloyArray{
public static void main(String[] args){
Person[] person = new Person[5];
person[0] = new Person("zhangsan",30);
person[1] = new Student("wangwu",18,74.0);
person[2] = new Student("wangpeng",17,89.5);
person[3] = new Teacher("zhangxiwang",32,15000);
person[4] = new Teacher("luoping",32,20000);
//遍历多态数组调用say方法
for (int i = 0; i < person.length; i++) {
System.out.println(person[i].say()); //动态绑定机制
if (person[i] instanceof Teacher){
((Teacher)person[i]).teach();
}
if (person[i] instanceof Student){
((Student)person[i]).study();
}
}
}
}
Person类
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;
}
}
Teacherr类
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(){
this.salary = salary;
}
@Override
public String say() {
return super.say() + " salary=" + salary;
}
public void teach(){
System.out.println("教师:" + getName());
}
}
Student类
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方法
public String say(){
return super.say() + " score=" + score;
}
public void study(){
System.out.println("学生" + getName());
}
}
运行效果
多态参数
- 多态参数:方法定的形参类型为父类类型,实参类型允许为子类类型
实例
需求:定义员工类Employee其中包含姓名name、月工资[private]以及计算年工资的getAnnual方法,普通员工和经理继承了员工,经理类多了奖金bonus属性和管理manage方法,普通员工类多了work方法,普通员工和经理类要求分别重写getAnnual方法
测试类中添加一个方法showEmpAnnual(employee e),实现获取任何员工对象的工资,并在main方法中调用该对象
测试类中添加一个方法testWork,如果是普通员工,则调用Work方法,如果是经理则调用manage方法
Employee类
public class Employee{
private String name;
private Double salary;
// 年工资
public double getAnnual(){
return 12 * salary;
}
public Employee(String name, Double salary){
this.name = name;
this.salary = salary;
}
public String getName(){
return name;
}
public Double getSalary(){
return salary;
}
public void setName(String name){
this.name = name;
}
public void setSalary(Double salary){
this.salary = salary;
}
}
Worker类
public class Worker extends Employee{
public Worker(String name,double salary){
super(name,salary);
}
public void work(){
System.out.println("员工" + getName() + "正在工作");
}
public double getAnnual(){
return super.getAnnual();
}
}
Manager类
public class Manager extends Employee{
private double bonus;
public void manage(){
System.out.println("经理" + getName() + "正在管理");
}
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 double getAnnual(){
return super.getAnnual() + bonus;
}
}
PloyParameter类
public class PloyParameter{
public static void main(String[] args){
Worker tom = new Worker("tom",10.0);
Manager zhangsan = new Manager("zhangsan",7000,10000);
PloyParameter ployParameter = new PloyParameter();
ployParameter.showEmpAnnual(tom);
ployParameter.showEmpAnnual(zhangsan);
ployParameter.testWork(tom);
ployParameter.testWork(zhangsan);
}
//添加一个方法showEmpAnnual(employee)
public void showEmpAnnual(Employee e){
System.out.println(e.getAnnual());
}
// 添加一个方法testWork,如果是普通员工,则调用testWork方法,如果是经理则调用manage方法
public void testWork(Employee e){
if (e instanceof Worker){
((Worker) e).work();
}else if(e instanceof Manager){
((Manager) e).manage();
}
}
}
运行效果