【发布时间】:2012-09-06 13:52:55
【问题描述】:
这段代码
static void writeTo(List<? super Apple> apples) {
apples.add(new Apple());
apples.add(new Jonathan());
}
这段代码的作者声明
参数 apples 是某种类型的 List,它是 Apple 的基本类型;因此您知道添加 Apple 或 Apple 的子类型是安全的。由于下界是苹果,
Jonathan 是 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) {}
两者有何不同?
另外让我对我发布的第一个代码感到困惑的是 我想 ? super T 表示 T 的任何基本类型。但从外观上看,他添加了 T 的子类型。我很困惑。
【问题讨论】:
-
无法理解问题。这两个函数似乎具有相同的签名。有什么问题?
-
@vainolo 我添加了一些关于错误的信息。请检查一下
标签: java generics wildcard super