java6:方法重載(overload)

1 重載方法(oveloading methods)

The max method that was used earlier works only with the int data type. But what if you

need to determine which of two floating-point numbers has the maximum value? The solution

is to create another method with the same name but different parameters, as shown in the fol-
lowing code:
public static double max(double num1, double num2) {
if (num1 > num2)
return num1;
else
return num2;
}

在上一篇中,我們使用了max(int n1,int n2)來取兩個int型的最大,但是如果我們需要比較的是兩個double型呢?或者說,我們要比較的數有三個呢?這時候就可以用重載方法。這樣,如果在你調用方法時候,傳入的參數是int,那麼max(int n1,init n2)就會被調用,如果傳入的參數是double型的,那麼max(double,double)的方法就會被調用,也就是是說,在方法調用時候,系統會先匹配下參數類型,個數來決定調用哪個方法,當然是調用最匹配的方法。

那麼,當我們傳入方法的參數是(int,double),這時候會成功調用函數嗎?怎麼調用呢?看下原文說法:

Can you invoke the max method with an int value and a double value, such as max(2,
2.5)? If so, which of the max methods is invoked? The answer to the first question is yes. The
answer to the second is that the max method for finding the maximum of two double values
is invoked. The argument value 2 is automatically converted into a double value and passed
to this method.
You may be wondering why the method max(double, double) is not invoked for the call
max(3, 4). Both  max(double, double) and max(int, int) are possible matches for
max(3, 4). The Java compiler finds the most specific method for a method invocation. Since
the method max(int, int) is more specific than max(double, double), max(int, int)
is used to invoke max(3, 4).

也就是說,答案是肯定也會調用方法的,但是他調用(double,double)的方法,其實覺得結合我們前邊在講到類型轉換中就會明白,就像我們將一個int與一個double相加時候,int會被自動轉爲double,而當我們調用的(int,double)時候,java 編譯器就找,如果能找到(int,double)的當然是最最匹配的,但是這時候找不到,只有(double,double)跟(int,int) ,當然是將int自動轉爲了double,然後調用double。同樣,看官可以將(int,int)型的方法給註釋掉,傳入(3,5)的調用就沒辦法退而求有就調用(double,double )的

請看下面的例子,特別注意第四第五個調用,體會這種最匹配調用辦法

public class TestOverLoading
{
	public static void main(String [] args)
	{
		System.out.println("the max of 3,5 is "+max(3,5));
		System.out.println("the max of 3.2,5.8 is "+max(3.2,5.8));
		System.out.println("the max of 3,9,7 is "+max(3,9,7));
		System.out.println("the max of 3,9.9 is "+max(3,9.9));
		System.out.println("the max of 3.0,9,7 is "+max(3.0,9,7));
	}
	public static int max(int n1,int n2)
	{
		System.out.print("max(int, int) is called, ");
		if(n1>n2) return n1;
		return n2;
	}
	public static double max(double n1,double n2)
	{
		System.out.print("max(double, double) is called, ");
		if(n1>n2) return n1;
		return n2;
	}
	public static int max(int n1,int n2,int n3)
	{
		System.out.print("max(int, int,int)is called, ");
		return (max(max(n1,n2),n3));
	}
		public static double max(double n1,double n2,double n3)
	{
		System.out.print("max(double, double,double)is called, ");
		double d=n1>n2?n1:n2;
		return d>n3?d:n3;
	}
}


Note
Overloaded methods must have different parameter lists. You cannot overload methods based on
different modifiers or return types.

注意 overloaded 方法必須在參數列表中有不同的。如果僅僅是修改返回值類型或者方法修飾符,是不能叫overload方法的

發佈了112 篇原創文章 · 獲贊 15 · 訪問量 22萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章