tan*_*rli 6 java generics enums types
鉴于以下设置:
public class TestType {
public static void main(String[] args) {
List<Constants> list = new ArrayList<>();
accept(list); //Does not compile
}
static void accept(Iterable<MyInterface> values) {
for (MyInterface value : values) {
value.doStuff();
}
}
}
interface MyInterface<T> {
T doStuff();
}
enum Constants implements MyInterface<Integer> {
ONE, TWO, THREE;
@Override
public Integer doStuff() {
return ordinal();
}
}
Run Code Online (Sandbox Code Playgroud)
为什么编译器不接受列表作为accept()的参数?
List延伸Iterable通过Collection,这不是问题.
另一方面,编译器告诉我
incompatible types: java.util.List<enums.Constants> cannot be converted to java.lang.Iterable<enums.MyInterface>
但常量是一个MyInterface ......不是吗?
| 归档时间: |
|
| 查看次数: |
149 次 |
| 最近记录: |