[英]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.