我有一个关于子类和超类的问题。在我正在处理的一个项目中,我有一个名为“Team”的超类和一些名为“TeamBlue”、“TeamRed”的子类……而且,所有这些子类中都有静态字段和方法。
我的问题是:如何将任何子类对象(TeamBlue 或 TeamRed)存储到“团队”对象中?如果这是有道理的。
这是我想要实现的一个例子:
Team team = new BlueTeam(); <-- 将任何颜色团队存储到“团队”对象中
这是我拥有的代码的简短版本:
class Team {
//empty class used for binding all the team classes
}
class BlueTeam extends Team {
public static List<String> players = new ArrayList<String>();
}
class PlayerData {
Team playerTeam;
public PlayerData(Team tm){
playerTeam = tm;
}
playerTeam.players // I want to access any subclass that is stored into this "myTeam" object and access its players list
}
class createData {
List<PlayerData> data = new ArrayList<PlayerData>();
// this is what I've tried but I get a null exception
Team team = new BlueTeam();
data.add(new PlayerData(team));
}
小怪兽爱吃肉
慕容森
相关分类