这是一个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());
Run Code Online (Sandbox Code Playgroud)
我有这个代码,我知道输出是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
}
Run Code Online (Sandbox Code Playgroud)