Cloneable 在 Java 中如何工作以及如何使用它? [英] How does Cloneable work in Java and how do I use it?

查看:20
本文介绍了Cloneable 在 Java 中如何工作以及如何使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道以下内容:

  1. Cloneable 意味着我们可以拥有对象的克隆或副本,通过实现 Cloneable 接口.有什么优点和这样做的坏处?
  2. 如果对象是一个对象,递归克隆是如何发生的?复合对象?
  1. Cloneable means we can have a clone or a copy of objects, by implementing the Cloneable interface. What are the advantages and disadvantages of doing that?
  2. How does the recursive cloning happen if the object is a composite object?

推荐答案

关于 Cloneable 你应该知道的第一件事是 - 不要使用它.

The first thing you should know about Cloneable is - don't use it.

使用Cloneable正确实现克隆是非常困难的,而且付出的努力是不值得的.

It is very hard to implement cloning with Cloneable right, and the effort is not worth it.

而不是使用其他一些选项,例如 apache-commons SerializationUtils(深度克隆)或 BeanUtils(浅克隆),或者简单地使用复制构造函数.

Instead of that use some other options, like apache-commons SerializationUtils (deep-clone) or BeanUtils (shallow-clone), or simply use a copy-constructor.

请参阅此处,了解 Josh Bloch 关于使用 Cloneable<进行克隆的观点/code>,这解释了该方法的许多缺点.(Joshua Bloch 是 Sun 的一名员工,并领导了许多 Java 功能的开发.)

See here for the views of Josh Bloch about cloning with Cloneable, which explains the many drawbacks of the approach. (Joshua Bloch was a Sun employee, and led the development of numerous Java features.)

这篇关于Cloneable 在 Java 中如何工作以及如何使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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