java的动态绑定机制|多态数组和 多态参数

91 阅读5分钟

java的动态绑定机制(非常非常重要)

1.当调用对象方法的时候,该方法会和该对象的内存地址/运行类型绑定。 2.当调用对象属性时,没有动态绑定机制,哪里声明,哪里使用。

package com.hspedu.extend;

public class PolyExercise {
    public static void main(String[] args) {
        //编译类型是A,运行类型是B
        A a = new B();//向上转型
        System.out.println(a.sum());//先去运行类型B,也就是去子类
        System.out.println(a.sum1());//先去运行类型B,也就是去子类
    }
}

//父类
class A{
    public int  i = 10;
    //动态绑定机制
    public int sum(){
        //这里getI()调用的是子类的getI()
        return getI() + 10;//20+10,当调用对象方法的时候,该方法会和该对象的内存地址/运行类型绑定
    }
    //这里i调用的就是父类的i
    public int sum1(){
        return i + 10;//10+10,当调用对象属性时,没有动态绑定机制,哪里声明,哪里使用
    }
    public int getI(){//父类
        return i;
    }
}
//子类
class B extends A {
    public int i = 20;
    //如果把这注销了
//    public int sum(){
//        return i +20;//40
//    }
    public int getI(){//子类
        return i;//当上面的sum()调用这里的对象属性i时,没有动态绑定机制,哪里声明,哪里使用,这里返回的就是20
    }
      //如果把这注销了
//    public int sum1(){
//        return i  + 10;//30
//    }
}

运行结果

运行结果

多态数组

数组的定义类型为父类类型,里面保存的实际元素类型为子类类型。

应用实例:现有一个继承结构如下:要求创建1个Person,Teacher对象,统一放在数组中,并调用每个对象 say方法。

应用实例升级:如何调用子类特有的方法,比如Teacher有一一个teach,Student有一个study,怎么调用?

两个很重要又总是记不住的点

instanceOf比较操作符,用于判断对象的运行类型是否为XX类型或XX类型的子类型;

向下转型:子类类型 引用名 = (子类类型) 父类引用

package com.hspedu.extend.PolyArray;

public class PolyArray {
    public static void main(String[] args) {
        Person[] persons = new Person[5];//数组初始化
        //统一放一个数组中,依次赋值
        persons[0] = new Person("hjh",12);
        persons[1] = new Student("ujyg",12,90.5);
        persons[2] = new Student("uyg",12,89.4);
        persons[3] = new Teacher("iuh",12,6786);
        persons[4] = new Teacher("o9u",12,6757);

        for (int i = 0; i < persons.length; i++) {
            //动态绑定机制
            //persons[i] 编译类型是Person,运行类型根据实际情况有JVM来判断
            System.out.println(persons[i].say());
            //instanceof比较符,用于判断persons[i]的运行类型是否为Student
            if(persons[i] instanceof Student){
                //向下转型:子类类型 引用名 = (子类类型) 父类引用
                Student student = (Student) persons[i];
                student.study();
            }else if(persons[i] instanceof Teacher){
                Teacher teacher = (Teacher) persons[i];
                teacher.teach();
            }else if(persons[i] instanceof Person){
                //不做处理
            }else {
                System.out.println("你的类型有误");
            }
        }
    }
}

父类子类分别如下:

package com.hspedu.extend.PolyArray;
//父类
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.extend.PolyArray;

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() + '\t' +"score=" + score;
    }

    public void study(){
        System.out.println("学生" + getName() + "在学习");
    }

}
package com.hspedu.extend.PolyArray;

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() + '\t' + "salary=" + salary;
    }

    public void teach(){
        System.out.println("老师" + getName() + "在上课");
    }
}

多态参数

应用实例;定义员工类Employee,包含姓名和月工资[private],以及计算年工资getAnnual 的方法。普通员工和经理继承了员工,经理类多了奖金bonus属性和管理manage方 法,普通员工类多了work方法,普通员工和经理类要求分别重写getAnnual方法。

测试类中添加一个方法showEmpAnnual(Employee e),实现获取任何员工对象的 年工资,并在main方法中调用该方法[e.getAnnual()]

测试类中添加一个方法,testWork,如果是普通员工,则调用work方法,如果是经 理,则调用manage方法

package com.hspedu.extend.PolyParameter;

public class EmployTest {
    public static void main(String[] args) {
        PutongEmployee p = new PutongEmployee("p"8690);
        manager m = new manager("fk"576856875);
        EmployTest employTest = new EmployTest();//这里创建实例化对象创建的是本类啊我是不是脑阔疼

        employTest.showEmpAnnual(p);
        employTest.showEmpAnnual(m);
        employTest.testWork(p);
        employTest.testWork(m);
    }

    //showEmpAnnual(Employee e)
    //实现获取任何员工对象的年工资,并在main方法中调用该方法
    public void showEmpAnnual(Employee e) {
        System.out.println(e.getAnnual());
    }

    public void testWork(Employee e){
        if(e instanceof PutongEmployee){
            ((PutongEmployee) e).work();
//            PutongEmployee putongEmployee = (PutongEmployee) e;
//            putongEmployee.work();
        }else if(e instanceof manager){
            ((manager) e).manage();
//            manager manage = (manager) e;
//            manage.manage();
        }else{
            System.out.println("不做处理");
        }
    }
}

父类子类分别如下:

package com.hspedu.extend.PolyParameter;
//父类
public class Employee {
    private String name;
    private double salary;//月工资

    public Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    public double getAnnual() {
        return 12 * 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;
    }

}
package com.hspedu.extend.PolyParameter;

public class PutongEmployee extends Employee {
    public PutongEmployee(String name, double salary) {
        super(name, salary);
    }
    public void work(){
        System.out.println("普通员工" + getName() + '\t' + "is working");
    }

    @Override
    public double getAnnual() {
        return super.getAnnual();
    }
}
package com.hspedu.extend.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;
    }

    public void manage(){
        System.out.println("经理" + getName() + '\t' + "is managing");
    }

    @Override
    public double getAnnual() {
        return super.getAnnual() + bonus;
    }
}

Object类详解

Object类

Object类

equals方法:只能判断引用类型

== 是一个比较运算符。 == :既可以判断基本类型,又可以判断引用类型。

练习题:判断两个Person对象的内容是否相等,如果俩属性值都一样,则返回true,反之返回false

package com.hspedu.extend.improve.equals;
//判断两个Person对象的内容是否相等,如果俩属性值都一样,则返回true
public class equalsExercise {
    public static void main(String[] args) {
        Person person1 = new Person("uguy",13,'男');
        Person person2 = new Person("uguy",13,'男');
        System.out.println(person1.equals(person2));//没重写之前是fale,因为这俩不是同一个对象
    }
}
class Person{
    private String name;
    private int age;
    private char gender;
    
    //重写Object的equals方法
    public boolean equals(Object obj){
        if(this == obj){//如果比较的俩对象是同一个,直接返回true
            return true;
        }
        //如果对象是一个Person
        if(obj instanceof Person){
            //进行向下转型,因为需要得到obj的各个属性
            Person p = (Person) obj;
            return this.name.equals(p.name)&&this.age == p.age&&this.gender == p.gender;
        }
        //如果不是Person,则直接false
        return false;
    }

    public Person(String name, int age, char gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    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 char getGender() {
        return gender;
    }

    public void setGender(char gender) {
        this.gender = gender;
    }
}

就到这了先啦啦啦。