1
我有一些問題,我不明白爲什麼它不工作。 我有一個父類:如何調用List <Child>的方法,但方法的參數是List <Parent>?
public abstract class Parent {
...
}
和2子類:
public class Child1 extends Parent {
...
}
public class Child2 extends Parent {
...
}
我有一個方法,這正與孩子名單。但應與類型Child1和CHILD2工作,所以我想它應該工作:
public static void doSomething(List<Parent> list) {
...
}
,這是我怎麼稱呼它:
List<Child1> children = dao.getChildren("1");
doSomething(children);
List<Child2> children2 = dao.getChildren("2");
doSomething(children2);
但它不工作,就說明這錯誤:
The method doSomething(List<Parent>) in the type MyClass is not applicable for the arguments (List<Child1>)
The method doSomething(List<Parent>) in the type MyClass is not applicable for the arguments (List<Child2>)
我該怎麼寫這段代碼?
doSomething()返回什麼?你的代碼不顯示返回類型 – Alex
我更新了,我很抱歉,但是那個方法沒有關係 – victorio
啊,我太快讀了這個問題,以爲你在做List children = doSomething(chidren)。在事後有點愚蠢:) –
Alex