我不明白这里出了什么问题。
import java.util.*;
class Main {
private static class SomeX {}
void doSomethingWithX(SomeX someX) {
Collection<? extends SomeX> colectionOfX = new ArrayList<>();
colectionOfX.add(someX);
}
}
javac
的意思是:
Main.java:8: error: method add in interface Collection<E> cannot be applied to given types;
colectionOfX.add(someX);
^
required: CAP#1
found: SomeX
reason: argument mismatch; SomeX cannot be converted to CAP#1
where E is a type-variable:
E extends Object declared in interface Collection
where CAP#1 is a fresh type-variable:
CAP#1 extends SomeX from capture of ? extends SomeX
据我理解,extends
将CAP#1的下限限制为SomeX
,并且SomeX
本身也应满足这个限制。但是这段代码有什么问题呢?(使用javac 1.8.0_102
编译)
SomeX
的实例添加到Collection<? extends SomeX>
中”的问题。在那个特定的操作中有什么问题? - Xtra Coder