1. instanceof
使用:对象引用名 instanceof 类名
作用:来判读引用的对象和类名是否兼容(是否继承该类,或爷爷辈的类)
例子:
Team team = new Team();
team.addMember(new Magicer("jwnming",6));
team.addMember(new Soldier("dogee",4));
team.addMember(new Magicer("wanggl",7));
System.out.print("
" + "团队成员:");
for(int i = 0;i < team.allRole.length;i++){
if (team.allRole[i] instanceof Role ) {
System.out.print(team.allRole[i].getName() + " ");
}
}
System.out.println("
" + "
" +"这个团队的总伤害值:" + team.attackSum());