我有一個實現兩個接口(特別是接口Serializable
和java.security.Key
)的對象。該對象的類還有兩個靜態方法:一個採用Serializable
對象,另一個採用Key
對象。下面是一些示例代碼:Java - 重載方法接受參數的接口。哪種方法被調用,爲什麼?
public class Subclass implements Serializable, java.security.Key{
public static void test(Serializable s) {
System.out.println("Ser");
}
public static void test(java.security.Key k) {
System.out.println("Key");
}
public static void main(String[]args){
test(new Subclass());
}
}
現在,當我運行上面的主要方法,不會引發錯誤和Java調用test(Key)
方法。但是,當我改變Serializable
接口別的東西(我改變了test(Serializable)
參數和改變Subclass
超級接口)到別的東西,我的IDE顯示我的錯誤:
The method
test(Key)
is ambiguous for the typeSubClass
.
我想知道什麼是,爲什麼Java允許這種方法與Serializable
界面模糊不清,而且沒有其他方法?
你使用的其他類型是什麼?這些*別的*的 –