scjp 传递对象
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在这一部分中,
变量
hotel
是一个新的局部变量,它接收参考值。这个新的局部变量在第一行中加载了对新Hotel
实例的新引用,并且返回了这个新引用。外部局部变量
h1
不会改变。In this part
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 newHotel
instance in the very first line and this new reference is returned.The outer local variable
h1
will not change.我在这里有点具体:不要说传递了引用,而是将其视为“按值传递的引用”。所以基本上,该方法接收指向所考虑对象的引用的副本。两个引用(原始
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 newhotel
) 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.
这是因为对象是“按值传递,而不是按引用传递”。
按值传递的是它的引用。所以,在你看来,你认为它是通过引用传递的。
因此,为了清楚起见,当您将对象传递给方法时,会创建一个新的“指针引用”,然后将其传递。所以如果你修改它,什么也不会发生。
编辑:这里有一些代码
看看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
Take a look at Core Java. Best book ever!
传递对该对象的引用,并且该引用是按值传递的。这意味着传递了引用的副本。您可以修改酒店的内容,这些更改将对调用者可见。但是,如果您将新的 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.