Clone()vs Copy构造函数 - 在java中推荐 [英] Clone() vs Copy constructor- which is recommended in java

查看:180
本文介绍了Clone()vs Copy构造函数 - 在java中推荐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

clone method vs copy constructor in java. which one is correct solution. where to use each case?

推荐答案

克隆已损坏,所以不要使用它。

Clone is broken, so dont use it.


对象类
的CLONE方法是一个有点神奇的方法,它可以执行
,而不是纯粹的Java方法可以用
做的:它产生一个其对象的
的相同副本。自从
编译器*的
Beta版本天以来,它一直存在于
原始对象超类中;和所有古老的
魔法一样,需要适当的
咒语来防止
的咒语意外地回火

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

这篇关于Clone()vs Copy构造函数 - 在java中推荐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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