【发布时间】:2016-05-06 21:52:26
【问题描述】:
public interface Parent{
}
public class Child implements Parent{
}
public <T extends Parent> Class<T> getClass(){
return Child.class; // compile error, add cast to Class<T>
}
我希望上面的代码没有错误,但是当我返回 Child.class 时出现编译错误。
【问题讨论】:
-
公开课 getClass(){返回Child.class; } 工作。但不明白为什么第一个版本不起作用。
-
如果你总是要返回
Child.class,你应该使用Class<Child>...只在有意义的地方使用泛型...
标签: java class generics interface