/*方法重載就是方法名稱相同,但參數的類型與參數的個數不同通過傳
遞參數的個數及類型不同以完成不同功能的方法調用
本例中使用add方法進行重載
*/
public class ChongZai{
public static void main(String arg[]){
int One = add(1,2);
float Two = add(1.0f,3.0f);
int Three = add(1,2,3);
System.out.println("One 結果是:" + One);
System.out.println("Tow 結果是:" + Two);
System.out.println("Three 結果是:" + Three) ;
}
public static int add(int x, int y){
int temp = 0;
temp = x + y;
return temp ;
}
public static float add(float x, float y){
float temp = 0;
temp = x + y;
return temp ;
}
public static int add(int x, int y, int z){
int temp = 0;
temp = x + y + z;
return temp ;
}
}
執行結果如下:
D:\java_study\第4章\010402_【第4章:數組與方法】_方法的聲明及使用>java ChongZai
One 結果是:3
Tow 結果是:4.0
Three 結果是:6