小编Joh*_*erg的帖子

由参数类型转换的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);
Run Code Online (Sandbox Code Playgroud)

java generics parameters casting

4
推荐指数
1
解决办法
2775
查看次数

标签 统计

casting ×1

generics ×1

java ×1

parameters ×1