(2.3.1 supplementary information)Extensive read
Java overload resolution-- Overload method Matching AlgorithmAs follows:
(1) Find all the methods that can be called. The method that can be called means that the number of parameters is equal to the number of real parameters, and the type of real parameters can be converted to the type of method parameters.
(2) If the type of the input parameter can be directly matched, this method is executed.
(3) If only one method can be called, execute this method.
(4) when more than one method can be called, make a judgment one by one: if the type signature of a method can be assigned to another method, the latter (type larger) will be excluded; repeat this operation until it cannot be ruled out.
(5) After step (4), if there is only one method left, this method is executed; or the compiler reports an error.
However, automatic packing and variable length parameters make the problem more complicated. So you can get a general idea.
Example 1:There are void M (object), void M (INT []), m (INT) methods,
Object OBJ = NULL;
M (OBJ );
Call m (object) according to (2)
Example 2:There are void M (object), void M (INT []), m (INT) methods,
M (null );
After M (object) is excluded according to (4), void M (INT []) is called.
Example 3:There are void M (object), void M (INT []), m (string) methods,
M (null );
After M (object) is excluded according to (4), void M (INT []) and M (string) cannot be excluded. According to (5), an error is reported during compilation.
Example 4:There are void F (double, float), void F (float, double) methods,
F (12, 9 );
According to (5), an error is reported during compilation.
Example 5:Void F (double, float), void F (float, double), andVoid F (double, INT)Method,
F (12, 9 );
According to (5), an error is reported during compilation. F (double, float)-f (float, double)
Example 6:Void F (double, float), void F (float, double), andVoid F (float, INT)Method,
F (12, 9 );
Call F (float, INT) according to (4)
Reload method Matching Algorithm