Overloaded method of Java
Methods Benpian explore in Java overloading. So, what is overloading it? First on a bunch of code:
package com.my.pac06;
/**
* @author Summerday
* @date 2019/12/2 19:46
*/
public class OverloadTest {
public static void main(String[] args) {
Overload overload = new Overload();
//调用public void test():无参数
overload.test();
//调用 public void test(String arg):arg= overloading...
overload.test("overloading...");
//调用public void test(int arg):arg= 10
overload.test(10);
//调用形参长度可变的方法
overload.test(new int[]{10});
//调用形参长度可变的方法
overload.test(1, 2, 3, 4, 5);
}
}
class Overload {
public void test() {
System.out.println("调用public void test():无参数");
}
public void test(String arg) {
System.out.println("调用 public void test(String arg):arg= " + arg);
}
//false:public int test(){}
//false: private void test(){}
public void test(int arg) {
System.out.println("调用public void test(int arg):arg= " + arg);
}
//false:public void test(int par)
//包含参数长度可变的情况
public void test(int... args) {
System.out.println("调用形参长度可变的方法");
}
}
We know that to create a method that contains a modifier, a number of return type, method name, parameter lists, etc., as long as the same class, the same method name two or more methods, different parameters , there have been heavy upload phenomenon.
It is well understood that the same method name, parameters can be different means that different types of parameter may be a number of different parameters . as follows:
//方法名相同,参数类型、个数均不同
public void test(){};
public void test(int arg){};
//方法名相同,参数类型不同
public void test(int arg){};
public void test(String arg){};
//方法名相同,参数个数不同
public void test(int arg){};
public void test(int... args){};
But the parameter name different not count! ! ! as follows:
//不能构成重载,因为方法名和参数类型个数均相同
public void test(int arg){};
public void test(int par){};
Only return values of different types can not be overloaded. as follows:
//仅仅返回值类型不同,不可重载
public void test(){};
public int test(){};
Only different modifiers, useless, as follows:
//仅仅修饰符不同,不可重载
public void test(){};
private void test(){};
If simultaneous parameter variable parameter and specify the number of cases, if the number of targeted, less directly targeted heavy-duty case; if not before considering the case of variable parameter, nothing to do with the position defined.
public void test(int arg) {
System.out.println("调用public void test(int arg):arg= " + arg);
}
//包含参数长度可变的情况
public void test(int... args) {
System.out.println("调用形参长度可变的方法");
}
//调用public void test(int arg):arg= 10
overload.test(10);
//调用形参长度可变的方法
overload.test(1, 2, 3, 4, 5);
Note: If you have to pass a number of the above situation, and let him perform the method of variable length parameter, can he converted to a number of memory array, a method can try:
//调用形参可变的方法
overload.test(new int[]{10});
But most of the time, do not recommend that method overloading parameters of variable length, it is not necessary.
Why only on the return values of different types can not constitute a reason for heavy-duty conditions:
Java method when you call, you can ignore the return value, that is, public int test(){}
and public void test(){}
the two methods at the time of the call, only need to write on test();
, and no mention of the return value, and cause confusion.