正在阅读Java Generics and Collections
本书。
//given
public static <T> void copy(List<? super T> dst, List<? extends T> src) {
....
}
现在,它说 - We could also declare the above method with several possible signatures.
public static <T> void copy(List<T> dst, List<T> src) 1
public static <T> void copy(List<T> dst, List<? extends T> src) 2
public static <T> void copy(List<? super T> dst, List<T> src) 3
public static <T> void copy(List<? super T> dst, List<? extends T> src) 4
其中第一个限制性太强,因为它只允许在目标和源具有完全相同的类型(understood
)时调用。
其余三个等效于使用隐式类型参数的调用(understood
- 类型推理算法将推断适当的类型),
Confusing part -
但显式类型参数不同。对于上面的示例调用,第二个签名仅在 type 参数Object
时有效,第三个签名仅在 type 参数Integer
时有效。
因为我尝试过的事情而感到困惑(下图)
public class AsList {
public static void main (String...a) {
List<Number> l4 = new ArrayList<Number>();
List<Object> l5 = new ArrayList<Object>();
List<Integer> l6 = new ArrayList<Integer>();
//type is inferred implicitly for the below call
copy(l4, Arrays.<Integer>asList(1, 2, 3));
//type is specified explicitly
AsList.<Number>copy(l4, Arrays.<Integer>asList(1, 2, 3)); \why? 5
AsList.<Object>copy(l5, Arrays.<Integer>asList(1, 2, 3)); 6
AsList.<Integer>copy(l6, Arrays.<Integer>asList(1, 2, 3)); \why? 7
}
public static <T> void copy(List<T> dst, List<? extends T> src) {
for (int i = 0; i < src.size(); i++) {
dst.add(src.get(i));
}
}
}
在这里,根据Confusing part
唯一的语句,6
应该执行,但5
和7
也在工作。为什么?
编辑Confusing part
中提到的示例调用如下
Collections.copy(objs, ints);
Collections.<Object>copy(objs, ints);
Collections.<Number>copy(objs, ints);
Collections.<Integer>copy(objs, ints);
在情况 5 中,形式参数是 List<Number>
和 List<? extends Number>
。在List<Number>
和List<Integer>
中通过是完全可以的。
在情况 7 中,形式参数是 List<Integer>
和 List<? extends Integer>
。再次通过List<Integer>
和List<Integer>
很好。