我对Java比较陌生,我正在编写一个类,该类将具有对象列表作为其属性之一。我想知道的是,这些对象是否能够访问作为其属性的对象的公共属性。抱歉,如果无法解析,不可能或无法理解...
我之前已经看过这个问题,但找不到任何解决方案,可能是因为我的措辞。
public class Enemy{
Attack[] attacks;
int[] stats;
public Enemy(int[] stats, Attack[] attacks) {
this.stats = stats;
this.attacks = attacks;
}
public attack(attackNo) {
this.attacks[attackNo].execute;
}
}
public class Attack{
public execute() {
/*Is there a way to access the Enemy object's stats attribute from here?
If not, how should I rewrite this code?*/
}
}
答案 0 :(得分:0)
将“ stats”作为参数传递给“ execute”:
public class Enemy{
Attack[] attacks;
int[] stats;
public Enemy(int[] stats, Attack[] attacks) {
this.stats = stats;
this.attacks = attacks;
}
public attack(attackNo) {
this.attacks[attackNo].execute(stats);
}
}
public class Attack{
public execute(int[] stats) {
}
}