java - Clone() vs Copy constructor - 在java中推荐

标签 java clone copy-constructor

java 中的克隆方法与复制构造函数。哪一个是正确的解决方案。每个案例在哪里使用?

最佳答案

克隆已损坏,请勿使用。

THE CLONE METHOD of the Object class is a somewhat magical method that does what no pure Java method could ever do: It produces an identical copy of its object. It has been present in the primordial Object superclass since the Beta-release days of the Java compiler*; and it, like all ancient magic, requires the appropriate incantation to prevent the spell from unexpectedly backfiring

首选复制对象的方法

Foo copyFoo (Foo foo){
  Foo f = new Foo();
  //for all properties in FOo
  f.set(foo.get());
  return f;
}

阅读更多 http://adtmag.com/articles/2000/01/18/effective-javaeffective-cloning.aspx

关于java - Clone() vs Copy constructor - 在java中推荐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2427883/

相关文章:

c++ - 在 C++ 中对字符串使用复制构造函数和/或赋值运算符时发生堆栈溢出

指向对象的指针数组的 C++ 复制构造函数

c++ - std::copy 和容器的复制构造函数之间是否存在任何性能差异?

java - 如何为字符串Java中的每个字符赋值

jquery - 克隆 Accordion 的功能与原始 Accordion 不同

java - 引用对象的实例变量

Java 的 Eclipse Galileo 的 "clone()"方法生成器

java - 在一行中插入多个值

java - Recyclerview 图像加载奇怪的行为

java - jni 使用 NewDirectByteBuffer 在 java 中看到随机的东西