static void writeTo(List<? super Apple> apples) {
apples.add(new Apple());
apples.add(new Jonathan());
}
这段代码的作者指出,
的论点苹果是一些类型,它是苹果公司的基本类型的列表;因此您知道添加Apple或Apple的子类型是安全的。由于下界是苹果,因此乔纳森是苹果的一个子类,
乔纳森是苹果的一个子类。
但当我这个
List<Jonathan> loj = new ArrayList<Jonathan>();
listSuper(loj);
它给了我这个错误
The method listSuper(List<? super Apple>) in the type Basket<T> is not applicable for the arguments (List<Jonathan>)
凡listSuper
看起来像这样
static void listSuper (List<? super Apple> cont) {}
如何两者有何不同?
另外,我发布的第一个代码是什么使我困惑的是我认为的 ?超T意味着任何基类型的T,但从它的外观来看,他添加了T的一个子类型。我很困惑。
不明白这个问题。这两个函数似乎都有相同的签名。问题是什么? – vainolo
@vainolo我添加了一些关于错误的信息。请检查一下 – KyelJmD
[Producer Extends,Consumer Super](http://stackoverflow.com/questions/2723397/java-generics-what-is-pecs/2723538#2723538) –