【发布时间】:2017-02-02 17:51:43
【问题描述】:
我不明白这里出了什么问题
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编译)
【问题讨论】: