Java泛型转换(按参数类型)

Java泛型转换(按参数类型),java,generics,parameters,casting,Java,Generics,Parameters,Casting,如果可能的话,在我想要实现的地方实现以下所需功能的好解决方案是什么: 将例程的返回类型强制转换为例程参数的类型,例如 //Foo和Bar是两种常见的 接口公共{} 接口Foo扩展了公共{} 接口栏扩展公共{} //示例接口 公共列表公共列表getFeatureByType(clazz类); // Foo and Bar are two types of Common interface Common{} interface Foo extends Common {} interface

如果可能的话,在我想要实现的地方实现以下所需功能的好解决方案是什么:

  • 将例程的返回类型强制转换为例程参数的类型,例如

//Foo和Bar是两种常见的
接口公共{}
接口Foo扩展了公共{}
接口栏扩展公共{}
//示例接口
公共列表<代码>公共列表getFeatureByType(clazz类);
// 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);
public <T extends Common> List<T> getFeatureByType(Class<T> clazz);