[英]java interfaces and parameters types
我正在嘗試通過接口參數化泛型類型,Eclipse告訴我未為T
類型實現abc()
方法。 當然,由於T
是接口,所以未實現,程序將在運行時確定T
實際含義。 因此,如果有人可以幫助我解決這個問題,我將不勝感激。
我有類似的東西:
interface myInterface {
String abc();
}
class myClass<T> implements myClassInterface<T> {
String myMethod() {
T myType;
return myType.abc(); // here it says that abc() is not implemented for the type T
}
}
public class Main{
public static void Main(String[] arg) {
myClassInterface<myInterface> something = new myClass<myInterface>;
}
}
如您所定義, T
是Object
類型。 相反,您myInterface
編譯器提示T
實際上是myInterface
。 您可以通過定義T
擴展myInterface
:
class myClass<T> implements myClassInterface<T extends myInterface>{
String myMethod(){
T myType;
return myType.abc();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.