作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在阅读 Maurice Naftalin 和 Philip Wadler 合着的 Java Generics and Collections 一书,在前两章中,我的脑袋被疑惑搞得一团糟。我无法找出答案。
通话中:
public static <T> void copy(List<? super T> dst, List<? extends T> src) {
for (int i = 0; i < src.size(); i++) {
dst.set(i, src.get(i));
}
}
List<Object> objs = Arrays.<Object>asList(2, 3.14, "four");
List<Integer> ints = Arrays.asList(5, 6);
Collections.copy(objs, ints);
assert objs.toString().equals("[5, 6, four]");
在调用函数“复制”期间:
第一个参数:?= 对象
第二个参数:?=Integer
但是 T 的数据类型是什么? jvm 是如何根据删除实现决定的?
书上说:在Collections.copy(obj,ints)这一行,取类型参数T为Number。调用是允许的,因为 objs 的类型是 List
在我的设置中,我试图有一个界面 Table继承自 Map (因为它主要用作 map 的包装器)。两个类继承自 Table - 本地和全局。全局的将有一个可变的映射,而本地的将有一个只有本地条目的映射。
Rust Nomicon 有 an entire section on variance除了关于 Box 的这一小节,我或多或少地理解了这一点和 Vec在 T 上(共同)变体. Box and Vec
我是一名优秀的程序员,十分优秀!