我正在创建一个战斗模拟游戏。我有一个攻击职业和一个战士职业。public class Attack { private String name; private int points; public Attack(String name, int points) { this.name= name; this.points =points; } //getters public String getName() { return name; } public int getPoints() { return points; } public String toString() { return ("Name of the attack = " + name + " damage = " + points) ; }}由于战士有不同的攻击,我不能使用静态,因为它会覆盖之前的攻击。Monster 类片段: public Attack[] getAttacks() { return attacks; }public void attack(String attackname, Warrior otherWarrior){// How would I access the attack from the class?}我如何才能访问攻击字段?谢谢。
1 回答
梦里花落0921
TA贡献1772条经验 获得超6个赞
正如您可以访问类的实例方法一样this Monster,您也可以访问类的实例方法Warrior:
public class Monster {
Attack[] attacks;
// ...
public Attack[] getAttacks(){
return attacks;
}
public void attack(String attackname, Warrior otherWarrior){
Attack[] monsterAttacks = this.getAttacks();
// Assuming Warrior has the method `getAttacks()`
Attack[] warriorAttacks = otherWarrior.getAttacks();
// ...
}
}
public class Warrior {
Attack[] attacks;
// ...
public Attack[] getAttacks() {
return attacks;
}
}
添加回答
举报
0/150
提交
取消