// 重載 : Overload, 也稱為過(guò)載
// 在同一個(gè)類(lèi)中, 方法名相同, 參數(shù)列表不同(參數(shù)的類(lèi)型不同, 參數(shù)的個(gè)數(shù)不同, 參數(shù)的順序不同)
public class OverLoadTest {
public static int add(int a, int b) {
int c = a + b;
return c;
}
public static double add(int a, double b) {
double c = a + b;
return c;
}
/* 不能和上面的方法重載, 變量名順序不同沒(méi)用, 必須是類(lèi)型順序不同才行.
public static double add(int b, double a) {
double c = a + b;
return c;
}*/
public static double add(double a, int b) {
double c = a + b;
return c;
}
// 參數(shù)兼容性好. 返回值的兼容性也好
public static double add(double a, double b, double c) {
int d = (int)(a + b + c);
return d;
}
public static void main(String[] args) {
// 重載方法調(diào)用時(shí), 依據(jù)實(shí)參類(lèi)型來(lái)定位方法.
int c = add(10, 20);
System.out.println(c);
double d = add(2.0, 3.8, 4.9);
System.out.println(d);
System.out.println(add(10, 3.8));
System.out.println(add(1, 2, 3));
}
}