Overload (reload selection method):
Java allows multiple methods in a class to have the same name, but when the names are the same, different parameters must be included. This is overloading. The compiler will select the correct method according to the actual situation. If the compiler looks for it, Compilation-time errors will occur without matching parameters or finding multiple possible matches. This process is called overloaded parsing.
Overloading includes: overloading of ordinary methods and overloading of constructor methods
Method: that is, a function (which we collectively call in the text "method"), is a fixed program segment, or a subroutine, which can realize fixed operation functions. Moreover, it also has an entrance and an exit. The so-called entrance means the various parameters brought by the function. Through this entry, we can pass the parameter value of the function into the subprogram for processing by the computer; the so-called exit means the function. The return value of , after the program block is executed, the return value passes the value back to the program that called it.
Generally, a class can have multiple overload methods, and different overload methods in the class can be designed according to different needs. Among them, the number of parameters, types, and even the order of different types of parameters are different overloads for the same method. For example, as shown in the figure, the String class we often see is a typical example.
PS This example is also correcting a ridiculous mistake I made when I shared my experience last time. Check the title again, it means it is correct, it is an overload, and it is not written as a rewrite.
Method/step
First, let's write a complete example. This is the most primitive method, as shown in the figure. We will continue to learn each step in the future and reload this method layer by layer.
1. At the beginning, let’s discuss whether the access modifier has an impact on today’s discussion. The answer is that the access modifier does not affect the overloading of the method. That is, they are not necessary conditions for constituting different methods. In layman's terms, the difference in methods has nothing to do with whether the access modifier is consistent. As shown in the figure, if the access modifier is inconsistent and the parameters are the same (or none of them have parameters), Java defaults to them as the same method, so the compilation cannot be passed and an error is reported.
2. After excluding access modifiers, let's discuss whether the difference in return value can be another way of overloading. Here, we will give three simple examples, that is, the return value is empty and the return value is It is two methods of int type. Finally, it is proved that the return value is not a necessary condition for overloading methods, as shown in the figure:
3. The only difference next is the parameters. Let's practice according to situations. The first one is different parameters. Yes, different parameters must be different methods. We can try it again, as shown in the figure:
4. In the third step, we are talking about different parameters. It is not the difference in naming parameters, but the difference in parameter types. Let's write them out and let the facts prove it. As shown in the figure, the parameter types are the same, and they are both integer types, but the names of formal parameters are different, so the compilation cannot be passed.
5. Also, the overload caused by different parameters. We can write another example, 1 parameterless method, 1 int parameter method, and two int parameter methods, as shown in the figure:
6. At this time, we have reached the key link. Is there any other type of different overloads? some! Imagine that the parameters are different and the numbers are the same. Is there any way to become different? Yes! Guessed correctly! The order is different! In different orders, there are still different methods, which also enables overloading. As shown in the picture:
Here is another simple example
int sum(int m, int n){return m + n;}int sum(int m,int n,int k){return m+n+k;}double sum(double m,double n){return m + n;}double sum(double m,double n,double k){return m+n+k;} The main implementation is the overloaded method sum, which has the following semantics:
sum(m,n): find the sum of integers m and n,
sum(m,n,k): Find the sum of integers m, n and k,
sum(m,n): Find the sum of real numbers m and n,
sum(m,n,k): Find the sum of real numbers m, n and k.
Finally, let’s summarize that overloading is only concerned with parameters, with parameters but no parameters, different parameter types, different number of parameters, and different parameter orders of different types, all of which can implement overloading of the method.