Dynamic binding of Java polymorphism

Dynamic binding of Java polymorphism

The first review: polymorphism is a very important feature of object-oriented programming, which makes the program more readable and extensible.

Type of reference variable

Variables that reference types have two types: compile time types and run-time types. (also called declarative type and actual type respectively) for a simple example:

//假设Student类是Person类的子类
Person p = new Student();

Compile time type

Method binding

Associating a method call with a method body is called binding.

Static binding

Binding before program execution is called static binding, also known as early binding. It is the default binding method in process oriented languages.

In Java, a method decorated with private, static and final (static and final will be summarized later) or constructor can accurately tell the compiler which method to call, that is, static binding.

Dynamic binding

Binding at runtime is based on the runtime type of the object, which is called dynamic binding or late binding. Of course, in Java, except for those methods that are statically bound, the calling method of other methods is dynamic binding.

public class DynamicBinding {
    //Object是所有类的超类,根据向上转型,该方法可以接受任何类型的对象
    public static void test(Object x) {
        System.out.println(x.toString());
    }

    public static void main(String[] args) {
        test(new PrimaryStudent());//Student
        test(new Student());//Student
        test(new Person());//Person
        test(new Object());//java.lang.Object@1b6d3586
    }
}

class Person extends Object {
    @Override
    public String toString() {
        return "Person";
    }
    public void run(){}
    public void count(int a){}
}

class Student extends Person {
    @Override
    public String toString() {
        return "Student";
    }
    public void jump(){}
}

class PrimaryStudent extends Student {
}

Method table

We can also find that test (New primarystudent()); The running result of is student. The result is obvious because there is no method overriding the parent class in the primarystudent class. If the method is called by dynamic binding, the virtual opportunity will first find a suitable method in this class. If not, it will always look for the parent class until it is found.

Person:
    //下面省略Object方法签名
    //xxx()-> Object.xxx()
    //方法签名->实际调用的方法
    toString()->Person.toString()
    run()->Person.run()
    count(int)->Person(int)
Student:
    //下面省略Object方法签名
    //xxx()-> Object.xxx()
    //方法签名->实际调用的方法
    toString()->Student.toString()
    jump()->Student.jump()
    run()->Person.run()
    count(int)->Person(int)
PrimaryStudentt:
    //下面省略Object方法签名
    //xxx()-> Object.xxx()
    //方法签名->实际调用的方法
    toString()->Student.toString()
    jump()->Student.jump()
    run()->Person.run()
    count(int)->Person(int)

Reference books: Thinking in Java, Java core technology Volume I

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>