任何人都可以請解釋爲什麼這段代碼會引發一個模糊的過載錯誤,當然整數方法更具體和適用?Var-args overloading
感謝,
斯內德
package object_orientation;
public class Ambiguous {
//ambiguous error compiler unsure whether boxing is needed or not
static void overload(Integer... d){
System.out.println("Integer");
}
static void overload(long... d){
System.out.println("Long");
}
public static void main(String a[]){
int i = 1;
overload(i);
}
}
看到「整數」與「int」不同。 Integer是一個包裝。 – fpe
好帖子在此 - http://stackoverflow.com/questions/2521293/bug-with-varargs-and-overloading –
如果你在你的重載方法中將'long ...'更改爲'long',它將會被編譯。我相信可變參數在編譯時轉換爲數組,所以我認爲這就是模糊發生的地方 –