Function to bind

First, the function bind

         Function to bind divided into static binding and dynamic binding. (Binding refers to the call)

    When the code is finished, can be used javap -c java .class files, view java bytecode compiler generated for us. (Disassembly process)

    To do: 1, first in the program edit screen, right-click and choose show in Explorer will be the generated code files are located

                       Catalog presented.

                      2, click the project name in the address bar, and find out the file, open and find the project name.

      3, find the class bytecode files will delete the address in the address bar, type cmd enter, there will be a command editor.

                      4. Enter Javap -c Enter the file name .class bytecode files will appear.

              

 

    In this bytecode instruction represents Line 6 invokespecia call the constructor

                                Line 10 invokestatic instruction that is statically bound to a method, call the function

                                Line 14 invokevirtual instruction that is bound to the dynamic method, call the function

  ♥ static methods are static-bound call, instance methods are dynamically bound call

   ♥ static binding, refers to the binding compile time, the compiler stage, calling this method is to determine the good, and never will not change

 

      Dynamic binding, refers to the binding runtime, that is, at compile time, which function to call here, is uncertain.

 

 

 1 class A{
 2     protected int a;
 3     private String str;
 4     private Integer data;
 5 
 6     public A(int val){        //构造函数
 7         System.out.println("A()");
 8         this.a = val;
 9     }
10 
11     public static void show(){     //静态方法
12         System.out.println("static A.show");
13     }
14 
15        public  void FUNC () {         // example method 
16          System.out.println ( "A.func instance" );
 . 17      }
 18 is      // This method is a method of the above overload 
. 19      public  void FUNC ( int Data) {     // because the same method name, return type of the same, only different parameter list 
20 is          System.out.println ( "instance A.func Data" );
 21 is      }
 22 is  }
 23 is  
24  class B the extends A {
 25  
26 is      public B ( int Val) {
 27          Super (Val);
28      }
 29  
30      public  static  void Show () {
 31 is          System.out.println ( "static B.show" );
 32      }
 33 is  
34 is      // constitutes relationship rewrite (overwrite) the 
35      public  void FUNC () {      // return type, method name, the same list of parameters, only different scopes 
36          System.out.println ( "B.func instance" );
 37 [      }
 38 is  }
 39  
40  
41 is  
42 is  public  class functions bound {
 43 is      public  static  void main ( String [] args) {
 44 is         // B new new B = B (20 is); // the invokespecial constructor
 45          // B.show (); // invokestatic static method
 46 is          // b.func (); // invokevirtual example method 
47          A = A new new B (20 is );
 48          A.show (); // a static binding A.show 
49          a.func (); //   dynamic binding instance B.func A.func 
50      }

 operation result:

 

 

Code analysis:

     The first line is because the new new object, JVM will automatically call the constructor of the object.

     The second line A.show () static binding at compile time, to determine a good method to invoke, so after running here, which calls the class method directly show which class a static method call ().

     The third line a.func () is dynamic binding at runtime to determine which method to call. A a = new B (20); sentence is to say a Type A newly defined variables a, then a new a new object of type B,

    And the object reference to the variable a, and where after a.func () operation, a machine identification FUNC () method is a dynamic method, requires dynamic binding, and then locate the object on the stack B by a,

    And then find the end of the address table by the method of the object B address type B method table, look inside, found func A class inherits the address in class B in () method has been overridden. and so

    At this time, access to FUNC () method is to be rewritten Class B. So print out is in class B.

 

 

 

 

Note :

1.

------- Figure I -------------------------------

  

 

 -------Figure II-------------------------------

 

—————————————————————————————————————— 

   Figure 1 illustrates: a base class reference can refer to a derived class object

   Figure II Description: derived classes can not refer to a base class object reference

(Image understanding: human compared to the base class, the derived class compared Professor, A a = new B (20); the description herein a person needs, it can be completely sent to the professor.

                   B b = new A (20); and the sentence error because a professor is required here, and you give to a person, that person may be just a student,

     And can not meet the demand. )

 

 

2 . The inheritance structure of the base and derived classes, often referred to as a top-down structure of inheritance , the inheritance hierarchy of the type of support only from the lower to the conversion,

   Top-down conversion is not supported. (That is a derived class [professors] can be converted into a base class [people], but the base class [people] not be able to be converted into a derived class [professor].)

 expansion:

        1, Final application scenarios are three:

               1.final int data = 10; may be used to define a constant

               2.final sealed class may be modified class is called, can not be inherited

       3.final可以修饰类的实例方法,称作密封方法,表示该方法不能再派生类中重写(覆盖)

 

        2,继承结构中,基类和派生类的方法通常有两种关系:重载和重写

            重载:在一个类作用域中,函数名相同,参数列表不同

            重写:作用域不同,在基类和派生类中,返回值相同,函数名相同,参数列表也相同

                  ( 重写指的是派生类方法表中,派生类提供的重写方法,把基类相应的方法的地址给重写了(覆盖了))

        3,当父类中的成员变量的访问权限为private时,说明子类不能访问该变量,但此时该变量仍会被子类继承

       下来,只是不能访问它。对于被继承下来的方法和变量,可直接用     对象 . 方法  或 对象 . 变量  来掉用。

Guess you like

Origin www.cnblogs.com/ljl150/p/11694429.html