如果可能的话,在我想要的地方实现以下所需的功能将是一个很好的解决方案:
// Foo and Bar are two types of Common
interface Common{}
interface Foo extends Common {}
interface Bar extends Common {}
// Example interface
public List<? extends Common> getFeatureByType(Class<? extends Common> clazz);
// Example of what I want to achieve by using the (or a similar) interface.
// Can this be achieve without using typecasting to (List<Bar>) or (List<Foo>)
// and @SuppressWarning("unchecked") ?
List<Bar> bars = getFeatureByType(Bar.class);
List<Foo> foos = getFeatureByType(Foo.class);
答案 0 :(得分:5)
public <T extends Common> List<T> getFeatureByType(Class<T> clazz);