[英]OOP/JAVA: Does assinging an object to another changes its attributes(class variables) too?
So my question is if an object A
has an attribute such as int var;
所以我的问题是对象
A
是否具有诸如int var;
类的属性int var;
and object B
has an attribute int var2
both of the object are instantiated from the same class using the same constructor. 并且对象
B
具有属性int var2
这两个对象都使用相同的构造函数从相同的类实例化。 For illustrations purposes lets say that var=3
and var2=0
Does assigning object A=B
make A.var==0
? 为了便于说明,可以说
var=3
和var2=0
分配对象A=B
是否使A.var==0
? please do motivate your answer for me. 请为我激励您的答案。
here is more code in case you need more: 这是更多代码,以防您需要更多:
public class CarSize{
public CarSize(int x)
{
this.var=x;
}
public void call()
{
doubleVar(this);
}
private void doubleVar(CarSize cz)
{
int vara= cz.getVar()*2;
CarSize c= new CarSize(vara);
cz=c;
}
public int getVar(){return this.var;}
private int var;
public static void main(String args[])
{
CarSize cz = new CarSize(3);
cz.call();
System.out.println(cz.getVar());
}
}
ANNOUNCEMENT 公告
people please check the code before attemtpting to explain to me what i already know, thanks a lot for your understanding. 人们请在尝试输入代码之前向我解释我已经知道的内容,非常感谢您的理解。
The problem why your code does not work as expected lies at this short one of code: 您的代码为何无法按预期运行的问题在于以下简短的代码之一:
cz=c;
Since CarSize
is a reference type, variables of CarSize
store "pointers" to CarSize
objects, not the CarSize
objects themselves. 由于
CarSize
是引用类型,变量CarSize
店“指针”,以CarSize
对象,而不是CarSize
对象本身。
When you are passing a car size as a parameter, you are passing a "pointer". 当您传递汽车尺寸作为参数时,您传递的是“指针”。
cz=c
makes the pointer stored in cz
point to the same object as that stored in c
. cz=c
使得存储在所述指针cz
指向相同的对象,存储在c
。
Now let's see how you call this method: 现在让我们看看如何调用此方法:
doubleVar(this);
this
refers to the cz
that is declared in the main method. this
是指在main方法中声明的cz
。 Again, this
points to a car size object. 同样,
this
指向汽车尺寸对象。 In the doubleVar
method, you made the parameter point to another object, but you only made the parameter point to something else. 在
doubleVar
方法中,将参数指向另一个对象,但仅使参数指向其他对象。 this
is never changed, neither did its fields. this
是永远不会改变的,它的领域也不会改变。
That's why you don't see the values doubling. 这就是为什么看不到值翻倍的原因。
To fix this, don't assign the parameter a new value. 要解决此问题,请不要为参数分配新值。 Instead, change
this
directly. 而是直接更改
this
。 Get rid of the parameter because that is not needed. 删除该参数,因为这不是必需的。
private void doubleVar()
{
this.var *= 2; // you can actually omit "this.", I put it there just to make it clear for you
}
You can assign a reference to an object to another object, you cannot assign an object to another object. 您可以将对一个对象的引用分配给另一个对象,不能将一个对象分配给另一个对象。 In your case both A and B would reference the same object previously referenced by only B. Object referenced previously by A will be taken by the garbage collector and cease to exist.
在您的情况下,A和B都将引用先前仅由B引用的同一对象。A先前引用的对象将由垃圾收集器获取并不再存在。
In Java, using the =
operator with non-primitives results in aliasing. 在Java中,将
=
运算符与非基元一起使用会导致混淆。 If you have two different objects of the same class, a
and b
, saying a = b;
如果您有两个相同类的不同对象,则
a
和b
表示a = b;
makes a
the same object as b
, and garbage collection will eventually destroy what used to be in a
. 做
a
相同的对象b
,和垃圾收集将最终破坏了什么曾经是a
。
So, yes, the field will be the same, but changing a
's value of the field will also change b
's value of the field because a
and b
are now the same. 所以,是的,本场将是相同的,但改变
a
“领域的价值也将改变b
”领域的价值,因为a
和b
和现在相同。
public class Test{
public static void main(String[] args){
object a = new object(3);
object b = new object(0);
a = b;
System.out.println(a.var);
}
}
public class object{
protected int var;
public object(int x){
var = x;
}
}
This is code of the situation you described exactly. 这是您确切描述的情况的代码。 The output here would be "0" because when a programmer defined object is set equal to another object of the same type it overwrites the reference from the original value of 3 to the new value of 0
此处的输出为“ 0”,因为当程序员定义的对象设置为等于另一个相同类型的对象时,它会将引用从原始值3覆盖为新值0。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.