• This is the 21st day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Java dynamic binding mechanism

  • Important Java mechanism: dynamic binding mechanism
  • Java dynamic binding mechanism:
  1. When an object method is called, it is bound to the object’s memory address/run type
  2. There is no dynamic binding mechanism when calling object properties, where to declare and where to use
package com.xdr630.dynamic_;

public class DynamicBinding {
    public static void main(String[] args) {
      	// a is of compile type A and run type B
      	// Attribute depends on the compile type and method depends on the run type.
        A a = new B();
        System.out.println(a.sum()); / / 40
        System.out.println(a.sum1()); / / 30}}class A { / / parent class
    public int i = 10;
    
    public int sum(a){
        return getI() + 10;
    }
    
    public int sum1(a){
        return i + 10;
    }
    
    public int getI(a){
        returni; }}class B extends A { 

    public int i = 20;
    
    public int sum(a) {
        return i + 20;
    }
    
    public int getI(a){
        return i;
    }
    
    public int sum1(a){
        return i + 10; }}Copy the code

  • Conclusion 1
  • Put the above subclass insum()Method comment out,mainIn the methoda.sum()Output what?
public class DynamicBinding {
    public static void main(String[] args) {
        // a is of compile type A and run type B
        A a = new B();
        System.out.println(a.sum()); / / 30System.out.println(a.sum1()); }}class A { / / parent class
    public int i = 10;
    // Dynamic binding mechanism:
    public int sum(a){
        return getI() + 10;/ / 20 + 10
    }

    public int sum1(a){
        return i + 10;
    }

    public int getI(a){/ / parent getI ()
        returni; }}class B extends A { / / subclass

    public int i = 20;
    
    public int getI(a){/ / subclass getI ()
        return i;
    }

    public int sum1(a){
        return i + 10; }}Copy the code
  • Code analysis:
  1. At this timeaThe run type of theB.a.sum()I’m going to look for the subclasssum()Method, while in the subclasssum()Methods are unregistered, so you have to go to the parent class to find themsum()Method,sumMethod to tune againgetI()Method, in which case both parent and child classes havegetI()Methods.
  2. becausea.sum()The run type of the methodB, according to the dynamic binding mechanism, so find the subclassgetI()Method, while ingetI()In thereturn ithisiProperty, no binding mechanism, declared in a subclass, returns 20, and then goes back to the parent classsum()Method,20 + 10So in the enda.sumThe output value of is30

  • Conclusion 2
  • Put the above subclass insum1()Method comment out,mainIn the methoda.sum1()Output what?
public class DynamicBinding {
    public static void main(String[] args) {
        // a is of compile type A and run type B
        A a = new B();
        System.out.println(a.sum());/ / 30
        System.out.println(a.sum1());/ / 20}}class A { / / parent class
    public int i = 10;
    // Dynamic binding mechanism:
    public int sum(a){
        return getI() + 10;/ / 20 + 10
    }

    public int sum1(a){
        return i + 10;
    }

    public int getI(a){/ / parent getI ()
        returni; }}class B extends A { / / subclass

    public int i = 20;
    
    public int getI(a){/ / subclass getI ()
        returni; }}Copy the code
  • Code analysis:
  1. aThe run type of theB, so from the subclassBStart looking, and at this pointsum1Methods are commented out, so you have to go to the parent classAIn thesum1Methods,sum1In the methodreturn i + 10And theiProperty, no dynamic binding mechanism, where to declare, where to use, at this pointiProperty is in the parent classA“, soi10, soa.sum1The output value of is20