Reload (Reload method):
Java allows multiple methods to have the same name in a class, but at the same time the same name must have different parameters, which is overloaded, the compiler will pick the correct method according to the actual situation, A compile-time error occurs when the compiler finds no matching parameters or finds multiple possible matches, a process known as overloaded parsing.
Overloads include: Overloading of common methods and overloading of construction methods
Method: That is, the function (which we call " method "), is a fixed program segment, or it is called a subroutine, it can implement fixed operation function. And, at the same time, with an entrance and an exit, the so-called entrance, is the function of the various parameters, we can pass the function of the parameter value into the subroutine for the computer processing; the so-called exit refers to the return value of the function, which, after execution of the program, returns the value back to the program that called it.
Typically, a class can have multiple overloaded methods that can be used to design different overloaded methods in a class based on different requirements. Among them, the number of parameters, type, and even the order of different types of parameters are different overloads of the same method. For example, the String class that we often see is a typical example.
P.S. This example also corrects a ridiculous mistake I made in the last time I shared my experience. Look again at the title, which is error-free, is overloaded, not written rewrite.
Method/Step
first, let's write a complete example. This is one of the most primitive methods. We will continue to study each step of the way, in-depth overloading this method.
1 . To begin, let's discuss whether the access modifier has an impact on today's discussion, and the answer is that the access modifier has no effect on the overloads of the method. That is, they are not necessary to form different methods. In layman's point, there is no relationship between the method and the access modifier. , if the access modifier is inconsistent and the parameters are the same ( or None of the arguments ) ,java defaults to the same method, so the compilation fails, and the error is not passed.
2, after the access modifier is excluded, let's discuss the difference between the return value can be overloaded another way, here, we simply give three examples, that is, the return value is NULL, the return value is int type two methods, and finally proved that The return value is not a requirement for an overloaded method.
3, the next only can be distinguished is the parameters. Let's practice in a divided situation. The first, different parameters, yes, different parameters must be different methods, we can try again,
4, the 3rd step we are talking about different parameters, not the name of the parameter is different, but refers to the different types of parameters, we write to let the facts prove it. , the parameter types are the same, all are integer types, but the names of the formal parameters are different, and the compilation fails.
5, Also, because of the number of different parameters caused by overloading. We can write another example,1 non-parametric method,1 int parameter method, two int parameter method,
6 . By this time, we have reached the key point. Excuse me, are there other types of different overloads? Some! Imagine, the parameters are different, the number is the same, there is no way to become the same? That's right! You guessed it! The order is different! In the case of different order, the method is still different, which also implements overloading.
Finally, we conclude that the overload is concerned only with parameters, there are no parameters, parameter types, different parameters, different types of parameter order, you can implement the method of overloading.
How Java implements overloading a method