Java对象复制最佳选择?

发布于 2024-09-07 12:43:25 字数 520 浏览 3 评论 0原文

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

我需要在Java中复制一个对象(即“按值而不是按引用”复制对象,以便新对象不仅仅是对旧对象的引用)。我厌倦了实现可克隆,并且更喜欢使用复制构造函数。但是,我需要复制的类有许多需要复制的成员变量(超过 100 个),因此向类添加一个新的构造函数只是为了复制(仅在我的应用程序的一部分中需要)似乎是一个糟糕的解决方案由于其巨大的长度。

有更好的解决方案吗?我应该只使用clone()吗?我可以创建一个复制构造函数,而不是一一复制所有字段吗?我可以反思性地这样做吗?谢谢。

我基本上只需要创建一个与旧对象相同的新对象,但更改了一些(大约 100 个中的 10 个)字段(但我仍然需要这两个对象......所以新对象不能是对老的)。我愿意接受任何建议。

Possible Duplicate:
How do I copy an object in Java?

I need to copy an object in Java (i.e. copy the object "by value not by reference" so that the new object is not just a reference to the old). I'm weary of implementing clonable and would prefer to use a copy constructor. However, the class I need to copy has MANY member variables that need to be copied (over 100) so adding a new constructor to the class just for copying (which is only needed in a 1 part of my application) seems like a poor solution due to its enourmous length.

Is there a better solution? Should I just use clone()? Can I create a copy constructor but rather than copying all the fields over 1 by 1 can I do it reflectively? Thanks.

I basically just need to create a new object that is the same as the old one, but with a few (about 10 out of 100) fields changed (but I still need both objects..so the new one cannot be a reference to the old one). I am open to any suggestions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深居我梦 2024-09-14 12:43:47

BeanUtils 方法基于反射,因此它们(相对)较慢。 推土机和其他类似工具也是如此。复制构造函数是完成任务的最快方法,因此,如果您不是为 Android 等平台编写代码,而每 kb 代码都是一个问题,那么您应该选择这种方式。如果唯一的问题是编写复制构造函数,您可以使用 Eclipse 插件,例如 JUtils

BeanUtils methods are based on reflection, so they are (relatively) slow. So is dozer and other similar tools. Copying constructor is the fastest way to do your task so if you're not coding for platform such as Android when each kb of code is an issue, you should choose that way. If the only problem is writing copy constructors, you can use Eclipse plugins such as JUtils.

总攻大人 2024-09-14 12:43:44

一种选择是使用序列化。它不是最快的,但可能是最安全的之一。查看这个上的代码地点:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectputStream;
import java.io.ObjectOutputStream;

public class DeepObjectCopy {

  public static Object clone(Object copyObject) {
    try {
      ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
      ObjectOutputStream oos = new ObjectOutputStream(baos);
      oos.writeObject(copyObject);
      ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
      ObjectputStream ois = new ObjectputStream(bais);
      Object deepCopy = ois.readObject();
      return deepCopy;
    } catch (IOException e) {
      e.printStackTrace();
    } catch(ClassNotFoundException e) {
      e.printStackTrace();
    }
    return null;
  }
}

one option can be to use a serialization. It's not the fastest one but probably one of the safest. Look at the code on this site:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectputStream;
import java.io.ObjectOutputStream;

public class DeepObjectCopy {

  public static Object clone(Object copyObject) {
    try {
      ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
      ObjectOutputStream oos = new ObjectOutputStream(baos);
      oos.writeObject(copyObject);
      ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
      ObjectputStream ois = new ObjectputStream(bais);
      Object deepCopy = ois.readObject();
      return deepCopy;
    } catch (IOException e) {
      e.printStackTrace();
    } catch(ClassNotFoundException e) {
      e.printStackTrace();
    }
    return null;
  }
}
爱的十字路口 2024-09-14 12:43:40

clone() 进行浅克隆 - 它仅复制第一级字段。您应该避免使用clone()Cloneable,因为正确实现它们非常困难,并且很可能会损坏某些东西,尽管不是立即可见的。请参阅 Joshua Bloch 对此的看法。

如果您想要深层复制 - 即克隆整个对象层次结构,我可以建议两个选项:

但是,如果您想要浅复制 - 即您仍然希望将所有字段复制到新对象,但字段和字段的字段(等等)指向它们的原始值,那么您可以使用 BeanUtils.clone(..) (来自 commons-beanutils)

除了克隆问题之外 - 拥有 100 个成员变量并不是一个好兆头。考虑将您的类拆分为多个类(更新:和多个表,使用外键。当然,如果适用的话。如果这些确实是同一对象的属性,那么很好)

clone() makes a shallow clone - it copies only the first level of fields. You should avoid using clone() and Cloneable, because it is very hard to implement it correctly, and it is very likely that something will be broken, although not immediately visible. See what Joshua Bloch says about this.

If you want deep copy - i.e. the whole object hierarchy being cloned, I can suggest two options:

If however, you want shallow copy - i.e. you still want all your fields copied to a new object, but the fields, and the fields' fields (etc..) point to their original values, then you can use BeanUtils.clone(..) (from commons-beanutils)

Apart from the cloning matter - having 100 member variables isn't a good sign. Consider splitting your class into multiple classes (update: and multiple tables, using foreign keys. Of course, if that is applicable. If these are truly properties of the same object, then fine)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文