我有以下 Java
类定义:
import java.util.*;
public class Test {
static public void copyTo(Iterator<? extends Number> it, List<? extends Number> out) {
while(it.hasNext())
out.add(it.next());
}
public static void main(String[] args) {
List<Integer> in = new ArrayList<Integer>();
for (int i = 1; i <= 3; i++) {
in.add(i);
}
Iterator<Integer> it = in.iterator();
List<Number> out = new ArrayList<Number>();
copyTo(it, out);
System.out.println(out.size());
}
就是这样,我定义了方法copyTo
使用 wildcards
在Java
.我定义 List<Number> out
但是Iterator<Integer> it
.我的想法是我可以将迭代器定义为 Iterator<? extends Number>
那将是类型匹配。然而事实并非如此:
Test.java:13: error: no suitable method found for add(Number)
out.add(it.next());
^
method List.add(int,CAP#1) is not applicable
(actual and formal argument lists differ in length)
method List.add(CAP#1) is not applicable
(actual argument Number cannot be converted to CAP#1 by method invocation conversion)
method Collection.add(CAP#1) is not applicable
(actual argument Number cannot be converted to CAP#1 by method invocation conversion)
where CAP#1 is a fresh type-variable:
CAP#1 extends Number from capture of ? extends Number
1 error
所以我继续为 copyTo
定义了另一个定义方法:
static public void copyTo(Iterator<? super Integer> it, List<? super Integer> out) {
while(it.hasNext())
out.add(it.next());
}
也不行。使用 wildcards
的正确说法是什么?在这种情况下?
最佳答案
首先,你想通过向方法本身添加一个类型变量来施加约束,因为通过使用通配符你不能在两个参数之间施加约束,然后你必须考虑方法中涉及的类型的变化:
- 你想要一个
Iterator<X>
作为输入其中X
至少是您要复制的数字类型的类型(或子类型) - 你想输出一个列表,其中
Y
至多是数字类型(或父类(super class)型)的类型
这些约束是不同的,必须以不同的方式表达:
static public <T> void copyTo(Iterator<? extends T> it, List<? super T> out) {
while(it.hasNext())
out.add(it.next());
}
这基本上是“我接受 Iterator
的 T
或 T
的子类型,我输出到 T
的列表或 T
的父类(super class)型”
关于Java 泛型和模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33586166/