一、多态数组
数组的定义类型为父类类型,里面保存的实际元素类型为子类类型。
1、应用实例
应用实例:现有一个继承结构如下:Student类 和 Teacher类继承Person类(属性private,name-String; age-int; say()-String)
要求创建1个Person对象、2个Student对象和2个Teacher对象,统一放在数组中,并调用say方法。
public class PolyArr01 {
public static void main(String[] args) {
Person[] persons = new Person[5];
persons[0] = new Person("jack",20);
persons[1] = new Student("tom",18,97);
persons[2] = new Student("jerry",19,66.5);
persons[3] = new Teacher("scott",30,20000);
persons[4] = new Teacher("harry",35,14000);
for (int i = 0; i < persons.length; i++) {
System.out.println(persons[i].say());
//persons[i].say();//动态绑定机制
//person[i]的编译类型是Person
//运行类型根据实际情况
}
}
}
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=" + getName()
+", age=" + getAge();
}
}
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="+getScore();
}
}
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="+getSalary();
}
}
2、应用升级
应用实例升级:如何调用子类特有的方法,比如Teacher有一个teach,Student有一个study怎么调用?
Person[] persons = new Person[5];
persons[0] = new Person("jack",20);
persons[1] = new Student("tom",18,97);
persons[2] = new Student("jerry",19,66.5);
persons[3] = new Teacher("scott",30,20000);
persons[4] = new Teacher("harry",35,14000);
//应用升级
for (int i = 0; i < persons.length; i++) {
if(persons[i] instanceof Student) {
((Student)persons[i]).study();
//Student student = (Student)persons[i];向下转型
//student.study();
} else if(persons[i] instanceof Teacher) {
((Teacher)persons[i]).teach();
} else {
persons[i].say();
}
}
二、多态参数
方法定义的形参类型为父类类型,实参类型允许为子类类型。
- 定义员工类Employee,包含姓名和月工资[private],以及计算年工资getAnnual的方法。普通员工和经理继承了员工,经理类多了奖金bonus属性和管理manage方法,普通员工类多了work方法,普通员工和经理类要求分别重写getAnnual方法。
- 测试类中添加一个方法showEmpAnnual(Employee e),实现获取任何员工对象的年工资,并在main方法中调用该方法。
- 测试类中添加一个方法,testWork,如果是普通员工,则调用work方法,如果是经理,调用manage方法(这里写错了)
package com.lxhedu.poly_.polyParameter;
import com.lxhedu.extend_.improve_.Student;
public class PolyParameterTest {
public static void main(String[] args) {
Worker worker = new Worker("worker01",5000.5);
Manager manager = new Manager("manager01",7000.5,1000);
Test t1 = new Test();
t1.showEmpAnnual(worker);
t1.showEmpAnnual(manager);
t1.testWork(worker);
t1.testWork(manager);
}
}
class Test {
public void showEmpAnnual(Employee e) {
System.out.println(e.getAnnual());
}
public void testWork(Employee e) {
if(e instanceof Worker) {
((Worker)e).work();
} else if(e instanceof Manager) {
((Manager)e).manage();
}
}
}
//Employee
class Employee {
private String name;
private double salary;
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getAnnual() {
return "月薪="+getSalary();
}
}
//员工
class Worker extends Employee {
public Worker(String name, double salary) {
super(name, salary);
}
public void work() {
System.out.println("员工"+super.getName()+"正在工作");
}
public String getAnnual() {
return super.getAnnual()+",员工年薪="+getSalary()*12;
}
}
//经理
class Manager extends Employee {
private double bonus;
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 void manage() {
System.out.println("经理"+super.getName()+"正在摸鱼");
}
public String getAnnual() {
return super.getAnnual()+",经理年薪="+(getSalary()*12+getBonus());
}
}