为什么输出这个?(JAVA)

NuN*_*uNu 1 java object

这是一个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,但我不完全确定原因.有谁能解释为什么?

Ami*_*shk 6

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)

  • 我正在写一些类似的东西.请发布一个完整的工作示例,主要方法等再现问题或问题.此外,修改不会修改Rectangle对象,因为除了子类化Rectangle对象之外,矩形中没有名为modify的方法.例如,this.y,"这个"指的是什么?是否有一个声明为y的局部变量?在你给出的例子中,它不是r3的y. (2认同)