簡體   English   中英

java接口和參數類型

[英]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>;
      }
}

如您所定義, TObject類型。 相反,您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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM