【发布时间】:2014-11-11 03:28:20
【问题描述】:
如果有人可以在这里指出问题和可能的解释,那将是非常有帮助的。
class Parent{}
public class ChildClass extends Parent{
/*Main class*/
public static void main(String[] args) {
ArrayList<ChildClass> a3 = new ArrayList<ChildClass>();
foo(a3);
}
/*another function*/
static void foo(ArrayList<Parent> obj) {
}
这会引发以下编译错误。
The method foo(ArrayList<Parent>) in the type ChildClass is not applicable for the arguments (ArrayList<ChildClass>)
多态的规则应该允许我这样做。正确的?毕竟 ChildClass IS-A Parent。似乎是什么问题?
【问题讨论】:
-
输入
static void foo(ArrayList<? extends Parent> obj) {}。 -
谢谢,但我想解释一下为什么它没有编译。
标签: java oop polymorphism