Implemented in Java polymorphism (on)

Polymorphic way to achieve the Java syntax is divided into two types: a heavy-duty, 2 rewrite, also known as overloading compile-time polymorphism, rewriting is a multi-state operation...

So when the bottom of exactly how to achieve polymorphism of it, by reading the "in-depth understanding of the Java Virtual Machine," this book (later referred to the book, if no special instructions are references to the book), to achieve polymorphism process have a certain understanding. The following is a record of learning content for future review.

Read, read, suddenly found a bit more content, divided into upper and lower, the major record overloaded knowledge, knowledge is at the point of rewriting.

Overload

Overload is depending on the parameter type of the method, the number of parameters, parameter order to achieve different namesake method call overloading is achieved by static assignment, then what is it static assignment, to show you examples in the book Code:

public class StaticDispatch {
    static abstract class Human {
    }

    static class Man extends Human {
    }

    static class Woman extends Human {
    }

    public void sayHello(Human guy) {
        System.out.println("hello,guy!");
    }

    public void sayHello(Man guy) {
        System.out.println("hello,gentleman!");
    }

    public void sayHello(Woman guy) {
        System.out.println("hello,lady!");
    }

    public static void main(String[] args) {
        Human man = new Man();
        Human woman = new Woman();
        StaticDispatch sr = new StaticDispatch();
        sr.sayHello(man);
        sr.sayHello(woman);
    }
}
//输出:
//hello,guy!
//hello,guy!

IDEA can be seen in the method is not called called gray, which can see the sample code during compilation has identified the method that will be called. Before understanding the static assignment, you need to familiarize yourself with the actual static type and type these two concepts.

Static type and the actual type

Human man = new Man();

HumanCalled static type of the variable (Static Type), or appearance known type (Apparent Type), Manreferred to the actual type of the variable (Actual Type).

The book has this to say:

Static type and the actual type can occur in a program change, the difference between the static type of change occurs only when using the static type of the variable itself will not be changed, and the final static type is known at compile time; the actual type result of changes to be ascertained at runtime, compiler compiler does not know at the time what the actual type of an object yes.

The book also example:

//实际类型变化
Human man = new Man();
man = new Woman();
// 个人理解:man 的原本的实际类型是 Man,当第 3 行执行时,man 的实际类型就变成了 Woman
//静态类型变化
sr.sayHello((Man) man);
// 个人理解:接着上一步,man 的静态类型是 Human,此时显式转换为 Man,作为 sayHello(Man guy)方法的参数
sr.sayHello((Woman) man);
// 个人理解:前面将 man 的静态类型转换为 Man,但是第 8 行方法中的 man 静态类型还是从 Human 转换成 Woman
// 最终,man 的静态类型还是声明时的 Human

For that part of the book, then, is to understand a little about the following is my personal understanding:

  1. First, the static type and are for the actual type of the variable terms, describes the properties of the variable, and the two properties will change;
  2. Static type refers to the type of the variable is declared when, actually refers to the type of a variable is assigned to type the right number when the variable assignment;
  3. Static type of change occurs only when used, here to note two points: 1) simply means that either statically typed variables constant, or is in the use of the variable changes; 2) final static type of the variable will not change, or when the type of the original statement.

StaticDispatchmain method of the class, the two calls to the incoming parameters static type sayHello method is the same, but the actual type of barrier, a result of the call is the same method. From this we can see that the compiler is determined according to the static method call type parameter, the static type of finish after the code is known, so to say overloaded before it was determined to run the code.

Taken bytecode main method:

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=4, args_size=1
      // 0xbb 创建一个对象,并将其引用值压入栈顶
         0: new           #7                  // class jvmlearn/StaticDispatch$Man
         // 0x5c 复制栈顶数值并将复制值压入栈顶
         3: dup
         // 0xb7 调用超类构造方法,实例初始化方法,私有方法
         // 这个指令会用掉当前栈顶的值,所以前面复制了一份
         4: invokespecial #8                  // Method jvmlearn/StaticDispatch$Man."<init>":()V
         // 0x4c 将栈顶引用型数值存入第二个本地变量
         // 可以看下面的局部变量表
         7: astore_1
         8: new           #9                  // class jvmlearn/StaticDispatch$Woman
        11: dup
        12: invokespecial #10                 // Method jvmlearn/StaticDispatch$Woman."<init>":()V
        15: astore_2
        16: new           #11                 // class jvmlearn/StaticDispatch
        19: dup
        20: invokespecial #12                 // Method "<init>":()V
        23: astore_3
        // 0x2d 将第四个引用类型本地变量推送至栈顶
        24: aload_3
        25: aload_1
        // 0xb6 调用实例方法
        // 这里可以直接看到参数是 Human 类型,34 行的代码也一样
        26: invokevirtual #13                 // Method sayHello:(Ljvmlearn/StaticDispatch$Human;)V
        29: aload_3
        30: aload_2
        31: invokevirtual #13                 // Method sayHello:(Ljvmlearn/StaticDispatch$Human;)V
        34: return
      LineNumberTable:// 行号表
        line 30: 0
        line 31: 8
        line 32: 16
        line 33: 24
        line 34: 29
        line 35: 34
      LocalVariableTable:// 局部变量表,存了 main 方法的参数和局部变量,静态方法第一个局部变量不是 this,也没有 this
        Start  Length  Slot  Name   Signature
            0      35     0  args   [Ljava/lang/String;
            8      27     1   man   Ljvmlearn/StaticDispatch$Human;
           16      19     2 woman   Ljvmlearn/StaticDispatch$Human;
           24      11     3    sr   Ljvmlearn/StaticDispatch;

Now back to the definition of static assignment, all rely on static type positioning method executed version of the dispatch operation is called static assignment, overloading typical application is the method of static dispatched.

Feature

Static assignment occurs at compile time by the compiler to determine which overloaded method, but this method is not overloaded uniquely determined. In fact the compiler just to find out all the methods currently overloaded inside most appropriate that one. The reason for this situation, explain the removal of the book:

Literals need not be defined, so literal without an explicit static type, its static type inference can only go by the rules and understand the language.

Below is sample code for this overload characteristics given in the book:

public class Overload {

    public static void sayHello(Object arg) {
        System.out.println("hello Object");
    }

    public static void sayHello(int arg) {
        System.out.println("hello int");
    }

    public static void sayHello(long arg) {
        System.out.println("hello long");
    }

    public static void sayHello(Character arg) {
        System.out.println("hello Character");
    }

    public static void sayHello(char arg) {
        System.out.println("hello char");
    }

    public static void sayHello(char... arg) {
        System.out.println("hello char ...");
    }

    public static void sayHello(Serializable arg) {
        System.out.println("hello Serializable");
    }

    public static void main(String[] args) {
        sayHello('a');
    }
}

Can be seen in the IDEA, the call is sayHello (char arg) method. If you comment out the method, the compiler does not complain, you can see the next method call is sayHello (int arg).

Can be further tested, constantly notes the current method calls, you can find the compiler looks rules overloaded methods, namely bottom-up automatic type conversion, to find bottom-up.

reference

  • "In-depth understanding of the Java Virtual Machine": Second Edition, 8.3.2 assignment: 1. Static assignment P -247

Guess you like

Origin www.cnblogs.com/magexi/p/11815902.html