我目前正在尝试制作一个玩家可以坐的模拟座椅。当前座位存储在一个数组中,其中包含 X、Y、Z 和 boolean 值(表示座位是否已满)。
这是一些预定义的席位。我希望能够访问该数组并在需要时将 false 更改为 true。
Seat[] seats = new Seat[]{
new Seat(-2,64,16,false),
new Seat(-3,64,16,false),
new Seat(-4,64,15,false)
};
这是我的 Seat 类。
public class Seat {
int x, y, z;
boolean isFull;
public Seat(int x, int y, int z, boolean isFull){
this.x = x;
this.y = y;
this.z = z;
this.isFull = isFull;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public int getZ(){
return z;
}
public boolean getIsFull(){
return isFull;
}
public void setX(int x){
this.x = x;
}
public void setY(int y){
this.y = y;
}
public void setIsFull(boolean isFull){
this.isFull = isFull;
}
}
最佳答案
像任何其他数组一样访问seat[]
并调用方法setIsFull()
。传递参数 true
将“填补”席位。
例如,seats[0].setIsFull(true);
将填充 seats[0]
处的座位。
关于java - 访问和更改预定义数组中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31601738/