在Java中复制对象 [英] Duplicating objects in Java

查看:109
本文介绍了在Java中复制对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解到,当您在Java中修改变量时,它不会更改它基于的变量

I learned that when you modify a variable in Java it doesn't change the variable it was based on

int a = new Integer(5);
int b = a;
b = b + b;
System.out.println(a); // 5 as expected
System.out.println(b); // 10 as expected

我假设对象类似。考虑这个类。

I assumed a similar thing for objects. Consider this class.

public class SomeObject {
    public String text;

    public SomeObject(String text) {
        this.setText(text);
    }

    public String getText() {
        return text;
    }   

    public void setText(String text) {
        this.text = text;
    }
}

我尝试使用此代码后感到困惑。

After I tried this code I got confused.

SomeObject s1 = new SomeObject("first");
SomeObject s2 = s1;
s2.setText("second");
System.out.println(s1.getText()); // second as UNexpected
System.out.println(s2.getText()); // second as expected

请向我解释为什么更改任何对象会影响另一个对象。我知道变量文本的值存储在两个对象的内存中的相同位置。

Please explain to me why changing any of the objects affects the other one. I understand that the value of variable text is stored in the same place in memory for both of the objects.

为什么变量的值是独立的但对象是相关的?

Why the values for variables are independent but correlated for objects?

另外,如何复制SomeObject,如果简单的赋值不起作用吗?

Also, how to duplicate SomeObject, if simple assignment does not do the job?

推荐答案

Java中的每个变量都是引用。所以当你这样做时

Every variable in Java is a reference. So when you do

SomeClass s2 = s1;

你只需将 s2 指向同一个对象as s1 指向。您实际上是将参考s1的值(指向 SomeClass 的实例)分配给s2。 如果修改 s1 s2 也将被修改(因为它指向相同的对象)。

you just point s2 to the same object as s1 points to. You are actually assigning the value of the reference s1 (which points to an instance of SomeClass) to s2. If you modify s1, s2 will be modified as well (because it points to the same object).

有一个例外,原始类型: int,double,float,boolean,char,byte,short,long 。它们按值存储。因此,当使用 = 时,您只需指定值,但它们不能指向同一个对象(因为它们不是引用)。这意味着

There is an exception, primitive types: int, double, float, boolean, char, byte, short, long. They are stored by value. So when using =, you only assign the value, but they can not point to the same object (because they are not references). This means that

int b = a;

仅将 b 的值设置为值 a 如果您更改 a b 将不会更改。

only sets the value of b to the value of a. If you change a, b will not change.

在一天结束时,一切都是按值分配,它只是引用的值而不是对象的值(除了上面提到的基本类型)。

At the end of the day, everything is assignment by value, it's just the value of the reference and not the value of the object (with the exception of primitive types as mentioned above).

所以在你的情况下,如果你想复制 s1 ,你可以这样做:

So in your case, if you want to make a copy of s1, you can do it like this:

SomeClass s1 = new SomeClass("first");
SomeClass s2 = new SomeClass(s1.getText());

或者,您可以将复制构造函数添加到 SomeClass 将实例作为参数并将其复制到自己的实例中。

Alternatively, you can add a copy constructor to SomeClass that takes an instance as argument and copies it into its own instance.

class SomeClass {
  private String text;
  // all your fields and methods go here

  public SomeClass(SomeClass copyInstance) {
    this.text = new String(copyInstance.text);
  }
}

有了这个,你可以很容易地复制一个对象: / p>

With this you can copy an object pretty easily:

SomeClass s2 = new SomeClass(s1);

这篇关于在Java中复制对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆