The overload of a method is one in which multiple methods with the same name, but with different parameters, can be defined in a class, and when invoked, the corresponding method is selected according to the unused parameter table.
Like what
public class Test {
void max (int a,int b) {
System.out.println (a>b? a:b);
}
void Max (double a,double b) {
System.out.println (a>b? a:b);
}
public static void Main (string[] args) {
Test t = new Test ();
T.max (3,4);
T.max (3.0,4.4);
}
The output is:
Construction methods can also overload
Let's look at one more example.
Class chongzai{public
void A (int a);
public void A (strting a);
public void A (int a,int b);
}
As the above is a overload and overload to meet the conditions:
1. Must be the same class
2. Method name (also can be called function) the same
3. Parameter type is different or the number of parameters is not the same
At the same time I also give the LZ to speak about the role of overloading it or the above example to say
Chongzai CZ =new Chongzai ();
CZ.A (1); Call a (int a);
CZ.A ("Passing Parameters"); Call a (String a)
cz.a (1,2); Call a (int a,int b)
We've already said which method to call, and the calling method is that the program determines which method is invoked according to the parameters you enter.
Overloaded features well, let's say you play the game. The number of people who can play a game is not sure how many persons it is. Then you can use the overload.
For example, a maximum of 3 people, you can define 3 parameters.
public void A (String a);
public void A (String a,string b);
public void A (String a,string b,string c);
Two people then you call the 2-parameter Method 3 people you're going to call the 3-parameter method and how to invoke the above has explained