Joh*_*erg 4 java generics parameters casting
什么,如果可能的话,将是一个很好的解决方案,以实现我想要的以下所需的功能:
// 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)
public <T extends Common> List<T> getFeatureByType(Class<T> clazz);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2775 次 |
| 最近记录: |