我想拥有一个类的多个实例,但是能够一次更改一个变量的所有非静态实例。也许这段代码会更清晰:
public class Example {
public int _x; //_x and _y are NOT static
public int _y;
Example (int x, int y) {
_x = x;
_y = y;
}
public void zeroThem() {
_x = 0;
_y = 0;
}
}
The point of the zeroThem()
method is that I might have
Example A = new Example(1, 2);
Example B = new Example(3, 4);
But once I call something like:
A.zeroThem();
The following will be true:
A._x == 0;
B._x == 0;
A._y == 0;
B._y == 0;
有没有办法使_x和_y保持静态?