爲什麼我無法將對象添加到集合中?由於B類的東西,因爲以下原因延伸A.無法將實現接口A的對象添加到Collection中<? extends A>
import java.util.*;
public class TestGeneric
{
public static void main(String[] args)
{
Collection<? extends A> collection = new ArrayList<A>();
A a = new B();
collection.add(a);
}
private static class B implements A {
public int getValue() { return 0; }
}
}
interface A { int getValue(); }
可能的重複[無法用通配符泛型添加值到Java集合](http://stackoverflow.com/questions/3716920/cant-add-value-to-the-java-collection-with-通配符 - 通用型)。另請參閱:[Java泛型:什麼是PECS?](http://stackoverflow.com/questions/2723397/java-generics-what-is-pecs) –