Discussion on the method of overload in Java

Reload (reload method):

Java allows multiple methods in a class to have the same name, but with the same name, they must have different parameters. This is overload. The compiler will select the correct method according to the actual situation. If the compiler cannot find the matching parameters or find multiple possible matches, compile time errors will occur. This process is called overload resolution.

Overloading includes: overloading of common methods and overloading of construction methods

Method: that is, function (we collectively refer to it as "method" ), is a fixed program segment, or called a subroutine, which can realize the fixed operation function in. Moreover, it also has an entry and an exit. The so-called entry is the parameters of the function. Through this entry, we can pass the parameter values of the function into the subroutine for computer processing; The so-called exit refers to the return value of a function. After the program segment is executed, the return value will be passed back to the program calling it.

Generally, a class can have multiple overloaded methods, and different overloaded methods in the class can be designed according to different requirements. Among them, the number of parameters, types, and even the order of parameters of different types are different overloads of the same method. For example, as shown in the figure, the string class we often see is a typical example.

P. S. this example is also correcting a ridiculous mistake I made when sharing my experience last time. Check the title again. It indicates that it is correct. It is overloaded and not rewritten.

Method / step

First, let's write a complete example. This is the most primitive method, as shown in the figure. We will learn from each step in the future and reload this method layer by layer.

1. Let's start by discussing whether the access modifier has an impact on today's discussion. The answer is that the access modifier has no impact on method overloading. That is, they are not necessary conditions for different methods. Generally speaking, different methods have nothing to do with whether the access modifiers are consistent. As shown in the figure, if the access modifiers are inconsistent and the parameters are the same (or there are no parameters), Java defaults to the same method, so the compilation fails and an error is reported.

2. After excluding the access modifier, let's discuss whether different return values can be another way of overloading. Here, we will simply give three examples, that is, two methods with empty return value and int return value. Finally, it is proved that the return value is not a necessary condition for overloading methods, as shown in the figure:

3. Next, the only difference is the parameter. Let's practice according to the situation. First, different parameters. Yes, different parameters must be different methods. We can try again, as shown in the figure:

4. In step 3, we are talking about different parameters, not different naming of parameters, but different parameter types. Let's write it out and let the facts prove it. As shown in the figure, the parameter types are the same, all of which are integer types, but the names of formal parameters are different, so the compilation cannot pass.

5. Also, overloading caused by different number of parameters. We can write another example, one parameterless method, one int parameter method and two int parameter methods, as shown in the figure:

6. At this point, we have reached the key link. Excuse me, are there any other types of different overloads? yes , we have! Imagine that the types of parameters are different and the number is the same. Is there any way to make them different? Right! You guessed right! The order is different! When the order is different, there are still different methods, which realizes overloading. As shown in the figure:

Let me give you a simple example

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, K): find the sum of real numbers m, N and K.

Finally, let's summarize that overloading only cares about parameters, with or without parameters, different parameter types, different number of parameters, and different order of parameters of different types can realize method overloading.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>