我会问...我们使用instanceof吗? 如果有一个类有超类数组,超类有两个子类..或更多
例如:类团队有一个人数组(聚合关系) 而班上的人是超级的 子类是班主任和班级沙发) 我应该在这种方法中使用increaseof Void add_person(person p){
答案 0 :(得分:0)
假设您的Team类采用以下形式:
class Team{
Person[] personArr;
...
}
您的Player和Coach类与Person类具有以下关系:
class Player extends Person{
...
}
class Coach extends Person{
...
}
你有以下物品:
Coach tom = new Coach();
Player dick = new Player();
Person harry = new Player();
Team wolves = new Team ([tom, dick, harry]); //assume constructor
以下是instanceof
的行为:
tom instanceof Coach //returns true
dick instanceof Player //returns true
tom instanceof Person //returns true
wolves instanceof Team //returns true
harry instanceof Person //returns true
harry instanceof Player //returns true
harry instanceof Coach //returns false