函数的重载:在同一个类中,可以共存多个相同名称的方法,但前提条件是这些方法必须在参数的个数或类型上不同(只是返回值不同不能通过编译器),构造函数同样可以重载。
例子:
public class Test {
public int sum(int a,int b) {
return a + b;
}
public int sum(int a,int b,int c) {
return a + b + c;
}
public int sum(int a,int b,int c,int d) {
return a + b + c + d;
}
public int sum(int a,int b,int c,int d,int e) {
return a + b + c + d + e;
}
public static void main(String[] args) {
Test t = new Test();
int a = 1;
int b = 2;
int c = 3;
int d = 4;
int e = 5;
System.out.println("Strat Test");
System.out.println(sum(a,b));
System.out.println(sum(a,b,c));
System.out.println(sum(a,b,c,d));
System.out.println(sum(a,b,c,d,e));
}
}