1
代碼是超載原始變量
public class TestOverload {
public static void print(Float f, double d) {
System.out.println("Float,double");
}
public static void print(float f, double d) {
System.out.println("float,double");
}
public static void print(int f, double d) {
System.out.println("int,double");
}
// public static void print(int f, float d) {
// System.out.println("int,float");
// }
public static void print(double d1, double d) {
System.out.println("double,double");
}
public static void print(float d1, float d) {
System.out.println("float,float");
}
public static void main(String[] args) {
TestOverload.print(2, 3.0);
TestOverload.print(2, 3.0f);//Compiler error:The method print(float, double) is ambiguous for the type TestOverload
}
}
爲什麼它被給錯誤,而是應該選擇打印(浮點D1,浮子d)
PS: 在上面的代碼中,如果我評論:
// public static void print(int f, double d) {
// System.out.println("int,double");
// }
然後打印(浮點D1,浮子d)被稱爲...
我想「 (float)3.0f「是多餘的,3.0f是一個沒有歧義的浮點數 – Jerome
@Jerome:正確! – dacwe
我試過@ jain007的代碼,我不能得到他的錯誤。我得到'int,float'打印,所以你一定是對的。但爲什麼?它依賴於JAVA版本嗎? – sp00m