问题的关键是,为什么这会导致编译时错误?
List<Collection> raws = new ArrayList<Collection>();
List<Collection<?>> c = raws; // error
Run Code Online (Sandbox Code Playgroud)
我理解为什么泛型一般不协变.如果我们能够分配List<Integer>
到List<Number>
,我们会暴露自己ClassCastExceptions异常:
List<Integer> ints = new ArrayList<Integer>();
List<Number> nums = ints; // compile-time error
nums.add(Double.valueOf(1.2));
Integer i = ints.get(0); // ClassCastException
Run Code Online (Sandbox Code Playgroud)
我们在第2行得到一个编译时错误,以避免我们从第4行的运行时错误.这是有道理的.
List<C>
至 List<C<?>>
但是这个怎么样:
List<Collection> rawLists = new ArrayList<Collection>();
List<Collection<?>> wildLists = rawLists; // compile-time error
// scenario 1: add to raw and get from wild
rawLists.add(new ArrayList<Integer>());
Collection<?> c1 = wildLists.get(0);
Object o1 = c1.iterator().next();
// scenario 2: add to wild and get from raw
wildLists.add(new ArrayList<String>());
Collection c2 = rawLists.get(0);
Object o2 = c2.iterator().next();
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,最终我只获得Object
没有强制转换的元素,因此我无法获得"神秘"的ClassCastException.
JLS中与此对应的部分是§4.10.2,所以我理解为什么编译器会给我错误; 我没有得到的是为什么规范以这种方式编写,以及(以避免投机/基于意见的答案),它是否实际上为我提供了编译时的安全性.
如果你想知道,这里是(一个精简版)的用例:
public Collection<T> readJsons(List<String> jsons, Class<T> clazz) {
List<T> list = new ArrayList<T>();
for (String json : jsons) {
T elem = jsonMapper.readAs(json, clazz);
list.add(elem);
}
return list;
}
// call site
List<GenericFoo<?>> foos = readJsons(GenericFoo.class); // error
Run Code Online (Sandbox Code Playgroud)
错误是因为GenericFoo.class
有类型Class<GenericFoo>
,而不是Class<GenericFoo<?>>
(§15.8.2).虽然我怀疑这是一个相关的原因,但我不确定为什么会这样.但无论如何,如果Class<GenericFoo>
可以通过隐式或明确的方式进行转换,那将不会成为问题Class<GenericFoo<?>>
.
归档时间: |
|
查看次数: |
168 次 |
最近记录: |