简体   繁体   English

Java对象混乱

[英]Java objects confusion

I am currently creating an Android app and thought that I had a problem with saving data internally , etc. It turned out that the problem had nothing to do with that, but with my understanding of Java. 我目前正在创建一个Android应用程序,并认为我在内部保存数据等存在问题。事实证明,问题与该问题无关,但与我对Java的理解有关。

The problem is that when I do 问题是当我这样做

myObject1 = myObject2;

and use myObject1 in myObject3 which might be a list, or whatever (in my case a Hashtable) and then change myObject2 , myObject3 gets changed accordingly, as well. 并使用myObject1myObject3这可能是一个列表,或任何(对我来说一个Hashtable),然后改变myObject2myObject3得到相应的改变,也是如此。 What is this called, and where can I learn more about it? 这叫什么,在哪里可以了解更多? How do I assign myObject2 to myObject1 so that myObject1 is completely "independent"? 我该如何分配myObject2myObject1使myObject1完全“独立”?

Variables that are Objects in Java are called references and refer to the same location in memory. 在Java中作为对象的变量称为引用,并且引用内存中的相同位置。 If you want two objects of the same type that don't refer to the same location in memory in need to allocate memory for them on your machine by using the new keyword. 如果您希望两个相同类型的对象在内存中没有引用相同的位置,则需要使用new关键字为它们在计算机上分配内存。

Below both variables myObject1 and myObject2 are references to an OBJECT1 object, but that don't exist at the same memory location: 在变量myObject1和myObject2之下都是对OBJECT1对象的引用,但它们不在同一内存位置:

OBJECT1 myObject1 = new OBJECT1();
OBJECT1 myObject2 = new OBJECT1();

If assigning an object to another is important you can look into the clone() method or use a copy constructor: 如果将一个对象分配给另一个对象很重要,则可以查看clone()方法或使用复制构造函数:

public OBJECT1(OBJECT1 toCopy)
{
    this.field1 = toCopy.field1; 
    this.field2 = toCopy.field2; 
    this.field3 = toCopy.field3; 
    this.field4 = toCopy.field4;     
}

Those variables are references to an object; 这些变量是对象的引用 think of each variable as the end of a string. 将每个变量都视为字符串的结尾。 The other end is tied to an object. 另一端绑在一个物体上。 If you assign a variable, you're tieing a new string onto an object. 如果分配变量,则将新字符串绑定到对象上。

To create a copy, you (unsurprisingly) need to create a copy. 要创建副本,您(毫无疑问)需要创建一个副本。 Sometimes, this is easy: there might be a copy constructor that lets you do this: 有时,这很容易:可能有一个复制构造函数可让您执行以下操作:

ArrayList<String> copy = new ArrayList<String>(oldArrayList);

Other times, you may be a method that makes a copy; 有时,您可能是制作副本的方法。 for example, some classes implement the clone() method: 例如,某些类实现了clone()方法:

Foo copy = (Foo) otherFoo.clone();

You just have to study the API of a class to find a way to copy an object. 您只需要研究类的API即可找到复制对象的方法。

That depends on what myObject is. 这取决于myObject是什么。 The word you are looking for is clone if you want to have an exact copy. 如果您想要一个精确的副本,那么您正在寻找的单词就是clone But not all Objects support clone, so you may have to build your own clone method that (as @Hunter) pointed out needs to allocate new memory through the new keyword. 但是并非所有对象都支持克隆,因此您可能必须构建自己的克隆方法(如@Hunter指出),该方法需要通过new关键字分配新内存。

So in Java or in any OOP language 所以用Java或任何OOP语言

if you consider the statement 如果您考虑该声明

Object obj = new Object()

obj is called the handle which actually points to the location where the actual Object is stored. obj称为句柄,它实际上指向实际对象存储的位置。

so when you do obj1 = obj you are getting 2 handles which are actually pointing to the same location 因此,当您执行obj1 = obj您将获得2个实际上指向相同位置的句柄

so if you do obj1.setSomething() it will be reflected on the obj.getSomething() call also. 因此,如果执行obj1.setSomething()它也会反映在obj.getSomething()调用中。

Variables contain references to objects. 变量包含对对象的引用 In other languages, references are often called pointers . 在其他语言中,引用通常称为指针 So doing myObject1 = myObject2; 这样做myObject1 = myObject2; makes the myObject1 variable reference the same object as the myObject2 variable. 使得myObject1变量引用相同的对象作为myObject2变量。

If you want to make a copy of an object, the best way is to implement it yourself, for example using a copy constructor: 如果要复制对象,最好的方法是自己实现,例如使用复制构造函数:

public MyClass(MyClass other) {
    this.foo = other.foo;
    this.bar = other.bar;
    ...
}

and thus do 因此做

myObject1 = new MyClass(myObject2);

When you assign an object o1 to another object o2 you make the o2 pointing to o1. 当将对象o1分配给另一个对象o2时,会使o2指向o1。 So whenever you change object o1 object o2 changes accordingly. 因此,无论何时更改对象o1,对象o2都会相应更改。

It happens the same with objects inside a list. 列表中的对象也是如此。 This is because in Java when you assing an object to another you don't copy the content but it is like you "share" it. 这是因为在Java中,当您将一个对象关联到另一个对象时,您不会复制内容,而是像“共享”它一样。

The only way to create another object indipendent from everything is using new and then copy each attribute. 从一切创建另一个独立的对象的唯一方法是使用new ,然后复制每个属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM