• 韩顺平-多态的应用及动态绑定机制


    🔎写在前面:instancof比较操作符

    用于判断断对象的类型是否为 XX 类型或 XX 类型的子类型(是运行类型还是编译类型呢?)

    🔎多态的动态绑定机制

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

    在这里插入图片描述
    当我们将B类中sum和sum1方法注销后,输出结果改变成30和20,这是因为在调用sum时B类中没有sum方法,且B extends A所以调用A中的sum方法,但是调用之前会先绑定B中的getl方法,对于机制的第一条,所以第一个输出变为30。

    📌例题1在这里插入图片描述

    📚 PolyArray(父类)

    package Poly_.PolyArray;
    
    public class Polyarray {
        public static void main(String[] args) {
            Person[] persons=new Person[5];
            persons[0]=new Person("jack",20);
            persons[1]=new Students("tom",18,100);
            persons[2]=new Students("xiaoma",20,99);
            persons[3]=new Teacher("wang",35,7000);
            persons[4]=new Teacher("scot",40,20000);
    
            for (int i=0;i
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    📚 Person类

    package Poly_.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;
        }
    
    
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    📚 Students类

    package Poly_.PolyArray;
    
    public class Students extends Person {
        private double score;
    
        public Students(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()+"正在听课");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    📚 Teacher类

    package Poly_.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()+" salary="+salary;
        }
        public void teach(){
            System.out.println("老师"+getName()+"正在讲课");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    📈结果:

    在这里插入图片描述

    📌例题2

    在这里插入图片描述

    📚 Employee类

    创建属性,名字和薪资
    创建构造器
    创建get和set方法
    package Poly_.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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    📚 Worker类

    继承父类Employee的名字和薪资
    创建构造器
    创建work方法,get父类的私有属性Annual
    package Poly_.PolyParameter;
    public class worker extends Employee {
        public worker(String name, double salary) {
            super(name, salary);
        }
        public void work(){
            System.out.println("  普通员工  "+getName()+"  正在工作....  ");
        }
    
        @Override
     //普通员工没有其他方法,直接调用父类方法
        public double getAnnual() {
            return super.getAnnual();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    📚 Manager类

    继承父类Employee的名字和薪资,创建自己的私有属性bouns;
    创建构造器;
    创建mange方法,get父类的私有属性Annua并且联合自己的bouns属性;
    package Poly_.PolyParameter;
    
    public class Manager extends Employee {
        private double bouns;
        public Manager(String name, double salary, double bouns) {
            super(name, salary);
            this.bouns = bouns;
        }
    
        public double getBouns() {
            return bouns;
        }
    
        public void setBouns(double bouns) {
            this.bouns = bouns;
        }
        public void manag(){
            System.out.println("经理 "+getName()+" 正在管理 ....");
        }
    
        @Override
        public double getAnnual() {
        //获取他的基本工资(从父类),并且加上他的奖金才是年薪
            return super.getAnnual()+bouns;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    📚 Test类(父类)

    
    package Poly_.PolyParameter;
    
    import javafx.concurrent.Worker;
    
    public class Test {
        public static void main(String[] args) {
        worker w=new worker("tom",2000);
        Manager manager=new Manager("米兰",5000,50000);
        Test test=new Test();
        test.showEmployAnnual(w);
        //传进去的manag会跟Manger绑定,有动态绑定机制,调用的是经理方法
        test.showEmployAnnual(manager);
        test.testwork(w);
        test.testwork(manager);
        }
        public void showEmployAnnual(Employee e ){
        //传进来是什么员工,就会调用相应员工的薪资
            System.out.println(e.getAnnual());
        }
        public void testwork(Employee e){
        //看其编译类型是什么,如果是普通工人,就将其转为worker方法
            if (e instanceof worker){
                ((worker)e).work()//向下转型
            }else if (e instanceof Manager){//如果 是manager就将其转为Manager方法
                ((Manager)e).manag();
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    在这里插入图片描述
    传进去w,运行对象为worker,执行
    传进去manger,运行对象为Manager,执行

    test.testwork(w);
    test.testwork(manager);
    }
    public void showEmployAnnual(Employee e ){
    //传进来是什么员工,就会调用相应员工的薪资
    System.out.println(e.getAnnual());
    }
    public void testwork(Employee e){
    //看其编译类型是什么,如果是普通工人,就将其转为worker方法
    if (e instanceof worker){
    ((worker)e).work()//向下转型
    }else if (e instanceof Manager){//如果 是manager就将其转为Manager方法
    ((Manager)e).manag();
    }
    }
    }

    📈结果:

    在这里插入图片描述

  • 相关阅读:
    Ribbon负载均衡
    前端实现ofd打印预览以及下载
    Pandas中的数据转换[细节]
    Android finishInputEvent 流程分析
    反射机制(Reflection)
    Semantic Kernel 入门系列:🍋Connector连接器
    LeetCode //C - 23. Merge k Sorted Lists
    基于Java毕业设计新闻推送系统源码+系统+mysql+lw文档+部署软件
    使用 pyspark 进行 Classification 的简单例子
    电脑Win11安装Autocad出现错误要如何处理
  • 原文地址:https://blog.csdn.net/yangqiqi1997/article/details/126208494