目录
一、动态绑定机制
二、多态的应用
一、多态数组
二、多态参数
非常重要:
1、当调用对象方法的时候,该方法会和该对象的内存地址/运行类型绑定
2、当调用对象属性时,没有动态绑定机制,哪里声明,那里使用
package com.hspedu.poly_.dynamic_;
public class DynamicBinding {
public static void main(String[] args) {
//向上转型,编译类型是A,运行类型是B
A a = new B();
//从子类-B类中开始查找
System.out.println(a.sum());//20+20 ---> 20+10
System.out.println(a.sum1());//20+10 ---> 10+10
}
}
class A {//父类
public int i = 10;
public int sum() {
return getI() + 10;//看运营类型调用方法
}
public int sum1() {
return i + 10;//在哪里声明属性,就调用哪里的属性
}
public int getI() {
return i;
}
}
class B extends A {//父类
public int i = 20;
// public int sum() {
// return i + 20;
// }
public int getI() {
return i;
}
// public int sum1() {
// return i + 10;
// }
}
数组的定义类型为父类类型, 里面保存的实际元素类型为子类类型
要求:现有一个继承结构如下: 要求创建 1 个 Person 对象、 2 个 Student 对象和 2 个 Teacher 对象, 统一放在数组中, 并调用每个对象say 方法.
应用实例升级: 如何调用子类特有的方法, 比如Teacher 有一个 teach , Student 有一个 study
怎么调用?
package com.hspedu.poly_.polyarr_;
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 + " 年龄:" + age;
}
}
package com.hspedu.poly_.polyarr_;
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;
}
@Override
public String say() {
return "学生" + super.say() + "成绩:" + score;
}
public void study(){
System.out.println("学生" + getName() + "正在学习java....");
}
}
package com.hspedu.poly_.polyarr_;
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;
}
@Override
public String say() {
return "老师" + super.say() + " 工资:" + salary;
}
public void teach(){
System.out.println("老师" + getName() + "正在教java....");
}
}
package com.hspedu.poly_.polyarr_;
public class PolyArray {
// 数组的定义类型为父类类型, 里面保存的实际元素类型为子类类型
// 应用实例:现有一个继承结构如下: 要求创建 1 个 Person 对象、 2 个 Student 对象和 2 个 Teacher 对象, 统一放在数组
// 中, 并调用每个对象的say 方法.
public static void main(String[] args) {
Person person[] = new Person[5];
person[0] = new Person("jack", 30);
person[1] = new Student("smith", 19, 78.5);
person[2] = new Student("rachel", 25, 89);
person[3] = new Teacher("frank", 35, 9000);
person[4] = new Teacher("joey", 43, 12000);
for (int i = 0; i < person.length; i++) {
//调用父类和子类共有的say()方法
System.out.println(person[i].say());
if (person[i] instanceof Teacher) {
((Teacher) person[i]).teach();//调用Teacher类特有的teach方法
} else if (person[i] instanceof Student) {
((Student) person[i]).study();// //调用Student类特有的study方法
} else if (person[i] instanceof Person) {
System.out.println("属于父类的对象,不需要调用子类特有的方法");
}else {
System.out.println("你的类型有误,请自己检查....");
}
}
}
}
package com.hspedu.poly_.polyparameter_;
public 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 void getAnnual(){
System.out.println("年工资:" + salary * 12);
}
}
package com.hspedu.poly_.polyparameter_;
public 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;
//愿闻其详,还好吧蝉过别枝,岑沈
}
@Override
public void getAnnual() {
System.out.println("经理" + getName() + "的年工资:" + (getSalary() * 12 + getBonus()));
}
public void manage(){
System.out.println("经理" + getName() + "在管理员工...");
}
}
package com.hspedu.poly_.polyparameter_;
public class Worker extends Employee{
public Worker(String name, double salary) {
super(name, salary);
}
@Override
public void getAnnual() {
System.out.print("普通员工" + getName() + "的");
super.getAnnual();
}
public void work(){
System.out.println("员工" + getName() + "正在工作...");
}
}
package com.hspedu.poly_.polyparameter_;
public class PolyParameter {
public static void main(String[] args) {
Employee worker = new Worker("jack",5000);
Employee manager = new Manager("smith",12000,50000);
Test test = new Test();
test.showEmpAnnual(worker);
test.showEmpAnnual(manager);
test.testWork(worker);
test.testWork(manager);
}
}
class Test{
public void showEmpAnnual(Employee e){
e.getAnnual();
}
public void testWork(Employee e){
if(e instanceof Worker){
((Worker)e).work();
}else if(e instanceof Manager){
((Manager)e).manage();
}else if(e instanceof Employee){
System.out.println("父类对象,不需要调用子类的方法");
}else{
System.out.println("你的类型有误,请重新判断...");
}
}
}