方法重載

    
	/*方法重載就是方法名稱相同,但參數的類型與參數的個數不同通過傳
	  遞參數的個數及類型不同以完成不同功能的方法調用
	  
	  本例中使用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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章