在以下情况下,我无法理解仿制药,请参阅下面的内联评论以解答我的问题:
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?
}
答案 0 :(得分:8)
//Intuitively I would expect this to mean that test is set containing objects
//that subclass AbstractGroup
Set<? extends AbstractGroup> test;
不,这意味着它是一组特定的?它扩展了AbstractGroup。你和编制者都没有办法知道那是什么?是的,所以你无法向该套装添加任何东西。
您可以将集合的值分配给AbstractGroup类型的变量,但不能相反。
相反,你需要这个:
Set<? super AbstractGroup> test;
这个原则有时被称为PECS,并且很好地解释了in this answer。
答案 1 :(得分:4)
Set<? extends AbstractGroup> test;
这意味着你的集合可以是扩展AbstractGroup
的任何对象的集合,但通常编译器不允许你向该集合添加内容(因为它无法判断你是否要添加例子) SubGroupB
到Set<SubGroupA>
等。)。
test = new HashSet<SubGroupA>()
您的实际集合只包含SubGroupA
类型的对象及其子类。
但是,编译器仍然不知道test
的内容是什么(见上文)。
外卡的要点是:您可以将任何集合分配给使用AbstractGroup
或子类参数化的变量,从而确保您可以在中投射所有对象到AbstractGroup
(编译器检查)。
如果你想拥有一个可以包含任何AbstractGroup
对象的集合,就不要使用通配符。
//this would compile (under the assumption that SubGroupA extends AbstractGroup)
Set<? extends AbstractGroup> test = new HashSet<SubGroupA>();
//this wouldn't compile, since the compiler doesn't know the type of test (it might change at runtime etc.)
test.add(new SubGroupA());
//this wouldn't compile since AbstractGroup and SubGroupA are not the exact same type (hence the wildcard would be needed here)
Set<AbstractGroup> test = new HashSet<SubGroupA>();
//this would compile
Set<AbstractGroup> test = new HashSet<AbstractGroup>();
test.add(new SubGroupA());
答案 2 :(得分:2)
这里不需要使用通配符在你的情况下,只需说
即可Set<AbstractGroup> test;
然后您可以将AbstractGroup的任何子类放入集合中。
此外,您似乎没有在上面的代码中初始化测试。