2010-08-09 10 views
4

ce que, si possible, serait une bonne solution pour implémenter la fonctionnalité souhaitée suivante où je veux:Java fonte générique par type de paramètre

  • Cast le type de retour d'une routine au type de paramètre de routine, par exemple,

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

Répondre

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

C'est génial, merci! –