Java static and dynamic dispatch assignment

Reference article: https: //blog.csdn.net/ns_code/article/details/17965867

 

public class StaticDispatch {

static abstract class Humnan {}

static class Man extends Humnan {}

static class Woman extends Humnan {}

public void hello(Humnan guy) {

System.out.println("hello, Humnan");

}

 

public void hello(Man guy) {

System.out.println("hello, Man");

}

 

public void hello(Woman guy) {

System.out.println("hello, Woman");

}

 

public static void main(String[] args) {

Humnan man = new Man();

Humnan woman = new Woman();

StaticDispatch dispatch = new StaticDispatch();

dispatch.hello(man);

dispatch.hello(woman);

}

}

 

The results of this code is run:

hello, Humnan  

hello, Humnan

 

This is a static assignment mechanism

All rely on static types to target the implementation of the action version of the method of dispatch, are called static assignment, the most typical application is the method polymorphism static assignment of overloading. Static assignment occurs at compile time, and therefore determine the static allocation of action is not actually executed by the virtual machine

Human man = new Man();

The above code "Human" variable called static type, back, "Man" referred to the actual type of the variable. Static type and the actual type can occur in some changes in the program, except that 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, but the actual the results of the type of change to be ascertained at runtime.

When calling hello () method, the caller methods are under the premise of dispatch, which overloaded version to use depends entirely on (parameter method is the amount of data cases) the number and type of data passed in the parameter. Code deliberately defines the same two static types, different types of real variables, visible compiler (not a virtual machine, because if it is made at the discretion of the static type, then it is determined at compile time) when overloaded by static type rather than the actual type of the argument as a basis for the determination. And compile static type is known, so the compile phase, Javac compiler determines which overloaded version to use based on the static type of the parameter. This is the most typical application of static assignment

Dynamic assignment

Another important manifestation of the dynamic dispatch and polymorphism - override method has a very close relationship. Subclasses override method calls up after the transition is a good illustration of an example of dynamic assignment. This is common, so there will not be analyzed with the sample program. When it is clear that, in the judgment execution method in the parent class or subclass in coverage, to determine if the static type, then no matter how carried upward transition, will only call methods in the parent class, but the reality is that, according to parent class instance of a different subclass, different methods are called override subclass, obviously, where to dispatch to perform the actual version of the method according to the type of the variable. And determine the actual need to determine the type of the program is running down, this is called dynamic dispatch at runtime based on the actual method of determining the type of assignment executed version of the process.

 Analytical method

Class file compilation process does not include a coupling step in a conventional compiler, all method calls are only symbolic references Class files stored inside, rather than the actual operation method in the address entry in the memory layout. This feature Java to bring a more powerful dynamic expansion of capacity, making it possible to determine the method of direct reference to certain objectives during a class operation, called Dynamic Link, also signed part of the methods used or cited for the first time in the class loading phase when converted to a direct reference, this transformation is called static resolution.

    Static resolution founded on the premise that: there is a version of the method call can be determined before the program actually executed, and calls the version of this method at runtime it is immutable. In other words, it is necessary to invoke the target finalized at the time of compiler, calling such methods called parsing.

    In the Java language, in line with "the compiler shows that run on immutable" This method requires mainly static methods and private methods into two categories, directly associated with the former type, which can not be accessed externally, both methods by inheritance or otherwise impossible to re-write the other versions, so they are suitable resolved at class loading stage.

   Java virtual machine providing a total of four byte instruction method call, namely:

invokestatic: call the static method.
invokespecial: call instance constructor <init> method, and private methods inherited methods.
invokevirtual: call all virtual methods.
invokeinterface: call interface method will then determine an object of this interface at runtime.
    As long as the method can be invokestatic and invokespecial command called, can be determined only in a version called resolution phase, in line with the conditions of static methods, private methods, instance constructors and methods parent class four categories, they will be loaded in the class the direct reference symbol reference for analytical methods. These methods may be referred to as a non-virtual method (method further includes final), in contrast, another method is called virtual methods (except the final method). Here we must explain the method under final, although the final call method using invokevirtual command, but because it can not cover, there is no other version, so no need to send the other recipients multi-state selection. Java language specification clearly illustrates the final method is a non-virtual methods.
    Resolution call must be a static process, it is completely determined at compile time, the symbol will involve references into direct reference to be determined in the resolution phase of class loading, not to run again delayed the completion. The dispatch call may be static or dynamic may, in accordance with (the caller of Parcel parameters and methods of approach referred to as the method) is based on the number of cases assigned amount can be divided into single and multiple dispatch dispatch. Combinations of two types of dispatch way constitute a static single assignment, multiple dispatch static, dynamic single assignment, dynamic dispatch multiple dispatch four kinds of situations.

Guess you like

Origin www.cnblogs.com/wangflower/p/12234006.html