2010-08-09 157 views
4

什么,如果可能的话,将是实现以下所需的功能,我想很好地解决了:Java的泛型施放参数类型

  • 蒙上了程序的返回类型的类型常规参数,例如,

// 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); 

回答

5
public <T extends Common> List<T> getFeatureByType(Class<T> clazz); 
+0

这是真棒,谢谢! – 2010-08-09 13:24:55