2010-08-09 213 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