java realization of the basic principles of inheritance

 

 

 Process method call

Looking for examples of the method to execute when it is from, can not find the time to actually start looking for the type of information object, and then locate the parent class type information.

Dynamic binding, and dynamic binding mechanism is implemented in accordance with the actual type of the object to find time to perform the method, the subtype that is not found then look parent.

 

Access to process variables

Access to variables are statically bound, either a variable or class instance variables. The code is demonstrated in class variables: bs and cs, access class variables through an object, the system will be converted to directly access class variables Base.s and Child.s.

 

Sample code:

Package Penalty for com.xc.xcspringboot.chapter4.chapter4_3_1; 

/ ** 
 * the Created by XC ON 2019/10/6 
 * Listing 4-7 demonstrates how inheritance works: Base class 
 * / 
public  class Base { 

    / ** 
     * Static variables S 
     * / 
    public  static  int S;
     / ** 
     * instance variables A 
     * / 
    Private  int A; 

    // static initializer block 
    static { 
        System.out.println ( "base class static block, S:" + S); 
        S =. 1 ; 
    } 

    // instance initializer block 
    { 
        System.out.println ( "base class instance block, A:" + A);
        A =. 1 ; 
    } 

    / ** 
     * constructor 
     * / 
    public Base () { 
        System.out.println ( "base class constructor, A:" + A); 
        A = 2 ; 
    } 

    / ** 
     * Method STEP 
     * / 
    protected  void STEP () { 
        System.out.println ( "Base S:" S + + ", A:" + A); 
    } 

    / ** 
     * method Action 
     * / 
    public  void Action () { 
        System.out.println ( "Start" ); 
        the STEP (); 
        System.out.println ( "End");
    }
}
Package com.xc.xcspringboot.chapter4.chapter4_3_1; 

/ ** 
 * XC ON 2019/10/6 the Created by 
 * Listing 4-8 demonstrates the principle of inheritance: Child classes 
 * / 
public  class Child the extends Base {
     / ** 
     * group and static variable name of the class S 
     * / 
    public  static  int S;
     / ** 
     * name of the class and instance variables of the base a 
     * / 
    Private  int a; 

    static { 
        System.out.println ( "static block of code subclass, s:" + S); 
        S = 10 ; 
    } 

    { 
        System.out.println ( "block subclass instance, A:" + A); 
        A= 10;
    }

    public Child() {
        System.out.println("子类构造方法, a: " + a);
        a = 20;
    }

    protected void step() {
        System.out.println("child s: " + s + ", a: " + a);
    }
}
package com.xc.xcspringboot.chapter4.chapter4_3_1;

/**
 * Created by xc on 2019/10/6
 */
public class Chapter4_3_1 {
    public static void main(String[] args) {
        System.out.println("---- new Child()");
        Child c = new Child();
        System.out.println("\n---- c.action()");
        c.action();
        Base b = c;
        System.out.println("\n---- b.action()");
        b.action();
        System.out.println("\n---- b.s: " + b.s);
        System.out.println("\n---- c.s: " + c.s);
    }
}

 

---- new new Child () 
base class static block, S: 0 
subclass static code block, S: 0 
base class instance block, a: 0 
base class constructor, a: . 1 
subclass instance block, a : 0 
subclass constructor, A: 10 

---- c.action () 
Start 
child S: 10, A: 20 is 
End

 ---- b.action () 
Start 
child S: 10, A: 20 is 
End

 - - bs: 1 

---- cs: 10

 

Guess you like

Origin www.cnblogs.com/ooo0/p/11627013.html