繁体   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