我有讓我的頭周圍的仿製藥在下列情況下的問題,請參見在線評論下面我的問題:泛型集合和通配符的Java
public void exampleMethod() {
//Intuitively I would expect this to mean that test is set containing objects
//that subclass AbstractGroup
Set<? extends AbstractGroup> test;
//Yet the compiler complains here and I do not understand why?
test.add(new AnyAbstractGroupSubGroup());
//I would guess that a method call such as this at runtime
test = new HashSet<SubGroupA>()
//would mean that only objects of subgroupA can be added to the collection, but then
//what is the point in using the wildcard in the first place?
}
抱怨,怎麼樣? – Raedwald