这是一个来自 Rectangle 类的实例方法,我们在其中修改矩形的 x 和 y 坐标及其宽度和高度
public void modify(int newX, int y, int width, int h) {
int x = newX;
this.y = y;
width = width;
this.height = height;
}
Rectangle r3 = new Rectangle(0, 0, 10, 10);
r3.modify(5, 5, 50, 50);
System.out.print(r3.getX() + " " + r3.getY() + " ");
System.out.println(r3.getWidth() + " " + r3.getHeight());
我有这段代码,我知道输出是 0 5 10 10 但我不完全确定为什么。谁能解释为什么?
最佳答案
public void modify(int newX, int y, int width, int h) {
int x = newX; // the value isn't saved to the class members
this.y = y; // this is saved, hence you see the change in the y value
width = width; // meaningless, the variable is overwritten with it's own value
this.height = height; // who is height? the function receives h
}
关于java - 为什么输出是这样的? ( java ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1768235/