我正在創建一個戰斗模擬游戲。我有一個攻擊職業和一個戰士職業。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
提交
取消