scjp 传递对象

发布于 2024-12-04 11:21:06 字数 758 浏览 0 评论 0原文

public class Hotel {
    private int roomNr;

    public Hotel(int roomNr) {
        this.roomNr = roomNr;
    }

    public int getRoomNr() {
        return this.roomNr;
    }

    static Hotel doStuff(Hotel hotel) {
        hotel = new Hotel(1);
        return hotel;
    }

    public static void main(String args[]) {
        Hotel h1 = new Hotel(100);
        System.out.print(h1.getRoomNr() + " ");
        Hotel h2 = doStuff(h1);
        System.out.print(h1.getRoomNr() + " ");
        System.out.print(h2.getRoomNr() + " ");
        h1 = doStuff(h2);
        System.out.print(h1.getRoomNr() + " ");
        System.out.print(h2.getRoomNr() + " ");
    }
}

为什么调用 doStuff(h1) 后 h1 没有改变? 据我了解,应该传递对对象的引用,并且在方法中应该将其替换为新对象。

public class Hotel {
    private int roomNr;

    public Hotel(int roomNr) {
        this.roomNr = roomNr;
    }

    public int getRoomNr() {
        return this.roomNr;
    }

    static Hotel doStuff(Hotel hotel) {
        hotel = new Hotel(1);
        return hotel;
    }

    public static void main(String args[]) {
        Hotel h1 = new Hotel(100);
        System.out.print(h1.getRoomNr() + " ");
        Hotel h2 = doStuff(h1);
        System.out.print(h1.getRoomNr() + " ");
        System.out.print(h2.getRoomNr() + " ");
        h1 = doStuff(h2);
        System.out.print(h1.getRoomNr() + " ");
        System.out.print(h2.getRoomNr() + " ");
    }
}

Why h1 don't change after calling doStuff(h1)?
As I understand reference to object should be passed, and in method it should be replaced to new object.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

╰ゝ天使的微笑 2024-12-11 11:21:06

在这一部分中,

static Hotel doStuff(Hotel hotel) {
    hotel = new Hotel(1);
    return hotel;
}

变量hotel是一个新的局部变量,它接收参考值。这个新的局部变量在第一行中加载了对新 Hotel 实例的新引用,并且返回了这个新引用

外部局部变量h1不会改变。


main:h1 = 0x0000100                     (the old Hotel's address)
       |
     copying
       |
        ------->  doStuff:hotel = 0x0000100  (the method call)
                  doStuff:hotel = 0x0000200  (the new Hotel's address)
                              |
                          copying
                              |
main:h2 = 0x0000200 <---------

In this part

static Hotel doStuff(Hotel hotel) {
    hotel = new Hotel(1);
    return hotel;
}

the variable hotel is a new local variable, that receives the reference value. This new local variable is loaded with a new reference to a new Hotel instance in the very first line and this new reference is returned.

The outer local variable h1 will not change.


main:h1 = 0x0000100                     (the old Hotel's address)
       |
     copying
       |
        ------->  doStuff:hotel = 0x0000100  (the method call)
                  doStuff:hotel = 0x0000200  (the new Hotel's address)
                              |
                          copying
                              |
main:h2 = 0x0000200 <---------
放低过去 2024-12-11 11:21:06

我在这里有点具体:不要说传递了引用,而是将其视为“按值传递的引用”。所以基本上,该方法接收指向所考虑对象的引用的副本。两个引用(原始 h1 和新 hotel)都指向同一对象,但仍然不同。在该方法中,您修改“引用”,而不是它引用的对象,从而修改结果。

这篇 可能是一本不错的读物,其中作者使用了不同语言的代码示例。

I would be a bit specific here: rather than saying a reference is passed, think of it as "reference being passed by value". So basically, the method receives a copy of the reference which points to the object in consideration. Both the references (the original h1 and the new hotel) point to the same object but are still different. In the method, you modify the "reference" and not the object referenced by it and hence the result.

A good read might be this one where the author uses code samples using different languages.

撩发小公举 2024-12-11 11:21:06

这是因为对象是“按值传递,而不是按引用传递”。

按值传递的是它的引用。所以,在你看来,你认为它是通过引用传递的。

因此,为了清楚起见,当您将对象传递给方法时,会创建一个新的“指针引用”,然后将其传递。所以如果你修改它,什么也不会发生。

编辑:这里有一些代码

Hotel h1 = new Hotel(100); // h1 holds a reference to a Hotel object in memory
System.out.print(h1.getRoomNr() + " ");
Hotel h2 = doStuff(h1); // when doStuff is called, a new reference pointing to the same object is made, but if you change it, nothing will happen

看看Core Java。有史以来最好的书!

It's becouse the object is "passed by value, not by reference".

What is passed by value, is it's reference. So, at your eyes you think it's passed by reference.

So, to make it clear, when you pass an object to a method, a new "pointer reference" is made, and that is passed. So if you modify it, nothing happens.

EDIT: Here some code

Hotel h1 = new Hotel(100); // h1 holds a reference to a Hotel object in memory
System.out.print(h1.getRoomNr() + " ");
Hotel h2 = doStuff(h1); // when doStuff is called, a new reference pointing to the same object is made, but if you change it, nothing will happen

Take a look at Core Java. Best book ever!

〆凄凉。 2024-12-11 11:21:06

传递对该对象的引用,并且该引用是按值传递的。这意味着传递了引用的副本。您可以修改酒店的内容,这些更改将对调用者可见。但是,如果您将新的 Hotel 分配给 hotel 参数,则只有原始引用的副本会发生更改。原来的参考仍然指向原来的酒店。

A reference to the object is passed, and this reference is passed by value. This means that a copy of the reference is passed. You may modify the contents of the hotel, and these changes will be visible to the caller. But if you assign a new Hotel to the hotel argument, only the copy of the original reference will be changed. The original reference will still point to the original Hotel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文