Reload method matching algorithm and reload Matching Algorithm
(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. [The Most Specific Method]
(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 (there is also the import static factor, in the next section 2.3.2 ). So you can get a general idea.
① First, the automatic packing and variable length parameters are not taken into account. ②, the automatic packing plus the automatic packing and variable length parameters are added.
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)
Http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12
What is method overload? What are the conditions required for method overloading?
To put it simply, it is:
1. The method name is the same.
2. Besides the parameter name and return value, there must be at least one difference.
(The parameter name and return value are different)
2:
Different parameter types, different number of parameters, and order of parameters (parameter types in different order must be different)
In conclusion, when you call these two methods, the system will clearly know which method you are calling.
Java constructor overload
Method overload:
1. must be in the same class
2. Same method name
3. the number or type (or order of parameters) of the method parameters are different.
Note:
1. parameter order refers to the order of parameter types, which is irrelevant to parameter names, such as show (int a, Stringa, int c) and show (int c, String B, int) is the same method, not method overload, because their parameter type order is the same as int, String, int
2. Method Overloading is irrelevant to the access permission modifier and method return value.
Call:
1. when a method parameter is filled in, it automatically determines the parameter type, parameter type order, and number, and then calls the corresponding overload method. When the corresponding overload method does not match, then the compilation error is returned directly.
Example:
Method 1. show ()
Method 2. show (int a, int B)
Method 3. show (int a, String B)
Method 4. show (String B, int)
Method 5. show (int B, int)
1. Method 1.2.3.4 is an overload method (1 and [234] have different numbers of parameters, 2 and [34] have different parameter types, and 3 and [4] have different order of parameter types)
2. Method 1.3.4.5 is an overload method (1 and [534] have different numbers of parameters, 5 and [34] have different parameter types, and 3 and [4] have different order of parameter types)
3. method 2 and 5 are not overloaded by the method. Because the parameter type order is the same, it is irrelevant to the parameter name.
4. Take method 1.2.3.4 as an example. show () will call method 1, show () will call method 2, show (1, "1") will call method 3
Show ("1", 1) calls Method 4
Bytes ----------------------------------------------------------------------------------------------------------------
The names of the two methods are test1 and test2, and the method names are different. It is certainly not a method overload, and the constructor name must be the same as the class name. It is impossible to create a constructor with different names.
2. Suppose test (int I, String s) {} test (String s, int I ){}
The two constructor methods are reloaded without compilation errors. Because the order of parameter types is different, the order of the first constructor parameter types is int, String, the type order of the second constructor parameter is String, int