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

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

问题描述

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

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

推荐答案

Clone 已损坏,请勿使用.

Clone is broken, so dont use it.

Object 类的克隆方法是一种有点神奇的方法没有任何纯 Java 方法可以做到做:它产生一个相同的副本它的对象.它已经出现在原始对象超类,因为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-java Effective-cloning.aspx

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

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