java - 为什么输出是这样的? ( java )

标签 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());

我有这段代码,我知道输出是 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/

相关文章:

Java - 俄语字符串和 JTextField,将字符串更改为 "????..."

java - 使用 Java 中的外部应用程序编辑文件并写回?

java - 将用户输入字符串中的数据添加到数组

javascript - 使用不同方法创建时创建的对象的差异

java - 将使用 SOAP 从 (C# .net) Web 服务接收的对象转换为 Android 中的字符串数组

javascript - 在 JavaScript 对象中创建变量时,代码如何知道在哪里添加新变量?

javascript - 构造函数内部的公共(public)属性与私有(private)属性

java - 线程 "main"java.lang.NumberFormatException : For input string "F:/TextFiles/loginaccs.txt" 中的异常

java - 如何在spring中引用另一个xml文件的bean

javascript - 使用 lodash 将数组转换为对象