嗯,你好,這是我的問題。更改屬性ArrayList <Class>到ArrayList <SubClass>在子類
我想知道,如果給定一個abstract class Foo
,有可能,不知何故,得到它的屬性ArrayList<Bar>
(酒吧是抽象的也行)只包含它所使用的Foo
每個子類中含有一定特定的子類。
例如,在一個具體的該class FooChild
extends Foo
,屬性ArrayList<Bar>
將現在僅包含具體class BarChild
(其extends Bar
)。
更具體地說,如果我有這段代碼:
public abstract class CharactersGroup {
private ArrayList<Character> characters;
public CharactersGroup() {
this(new ArrayList<Character>());
}
public CharactersGroup(ArrayList<Character> list) {
characters = list;
}
}
和CharactersGroup
一個子類是HeroesGroup
,像這樣:
public class HeroesGroup extends CharactersGroup {
// Here is an ArrayList<Character> characters, that I would like to "become" an ArrayList<Hero> characters
public HeroesGroup() {
this(new ArrayList<Hero>());
}
public HeroesGroup(ArrayList<Hero> list) {
super(list);
}
}
一個Character
將只是想:
And a Hero
like:
public class Hero extends Character {
private int level;
public Hero(String n, int l) {
super(n);
level = l;
}
}