2

可能重复:
如何在 Java 中复制对象?

如何在 java 中初始化一个对象(比如 A)并将其初始成员值设置为等于第二个对象(比如 B)。初始化后我想修改A的成员而不修改B的成员。所以在AI初始化时只想复制B的数据。这是如何以一种好的方式完成的?

4

4 回答 4

1

您可以实现和使用克隆

MyClass b = new MyClass();
MyClass a = b.clone();

注意:有些类是不可克隆的,或者有损坏的实现。例如,只有当它们应该是深拷贝时才具有浅拷贝。

如果类是Serializable你可以序列化它并在内存中反序列化它。不完全是nice,但它有效。

或者您可以创建自己的“复制”构造函数。

于 2012-06-28T16:16:24.820 回答
0

这一切都取决于成员的类型。我举个例子:

class A
{
    public float value;
    public int[] anArray;

    public A(B b)
    {
        //primitive may be assigned directly.
        this.value = b.value;

        // other types different approaches:

        //copy the contents of the array
        this.anArray = new int[b.anArray.length];
        System.arraycopy(b.anArray, 0, this.anArray, 0, b.anArray.length);
    }
}

class B
{
    float value;
    int[] anArray;
    public B(int size)
    {
        this.value = 3f;
        this.anArray = new int[size];
        for (int i = size - 1; i >= 0; i--)
        {
            this.anArray[i] = i * 10;
        }
    }
}

B b = new B(5);
A a = new A(b);
于 2012-06-28T16:33:50.490 回答
0

克隆是一个简单的复制选项。如果您想做一些需要更多控制的事情,请创建您自己的方法来完全按照您的需要执行您的副本:

public MyType copy()
{
  MyType a = new MyType();
  // Initialize how you need to here, use the object this was called from if you'd like
  a.property = this.property;
  // etc.  
  return a;
}

这为您提供了更直接的控制,但需要更多时间来编写代码。如果克隆适合您的目的,请坚持下去。

编辑:我将根据您对此答案的评论举一个例子。

假设我们有以下类型:

TypeA: has the following member variables
int number = 5; // Default value built in by constructor.
int letter = 'x'; // Value is 'a' when constructed but has been changed.
ArrayList<TypeB> list = {b1, b2, b3} // All are initialized.

TypeB: has the following member variables
double decimal = 5.32
TypeC someObject = ...

TypeC: has some stuff, but we are going to ignore it.

现在,当我们要复制 TypeA 时,我们必须执行以下操作:

  1. 直接复制数字和字符,因为它们是值类型。
  2. 复制对 ArrayList 的引用,其中包含对某些 TypeB 的引用。

幸运的是,这些都是简单的步骤。

int copyNumber = this.number;
char copyChar = this.letter;
ArrayList<TypeB> copyList = this.list;
return new TypeA(copyNumber, copyChar, copyList);

现在假设一个特定的构造函数接受这三个参数,但希望你明白这一点。

如果您只想获取值,而不是对 ArrayList 中所有 TypeB 的引用,这将变得很棘手。您必须遍历 ArrayList 并创建复制所有 ITS 值的新 TypeB(双精度对象和 TypeC 对象作为引用或值...)

简而言之,您想要的是更容易执行的副本。简单的赋值运算符使用原始类型复制值并使用对象复制引用。

于 2012-06-28T16:19:54.890 回答
0

一种可能的解决方案是clone在您的类上实现方法并使用克隆,如下所示:

MyClass a = new MyClass();
MyClass b = a;

您会注意到这clone()并不是真正的公共方法,因此您需要公开它。此外,您需要告诉 Java 您的对象是可克隆的(这样做是为了让您的类实现Cloneable)。以下代码说明了它:

public class MyClass implements Cloneable {

    @Override
    protected MyClass clone() throws CloneNotSupportedException {
        return (MyClass)super.clone();
    }

}
于 2012-06-28T16:21:28.827 回答