java中的clone()是浅拷贝吗?
java中的clone()
是浅拷贝吗?
最终到达克隆() Object 的方法(最上面的 类),它创建一个新实例 与对象属于同一类,并且 将所有字段复制到新字段 实例(“浅拷贝”)。
我从维基百科读到了这篇文章。
我不明白为什么它是浅拷贝。 clone()
将创建一个包含所有字段的新实例。这只是深拷贝吗?使困惑。需要给我一些解释。
Is clone()
in java a shallow copy?
Eventually this gets to the clone()
method of Object (the uppermost
class), which creates a new instance
of the same class as the object and
copies all the fields to the new
instance (a "shallow copy").
I read this from wikipedia.
I don't understand why it is a shallow copy. clone()
will create a new instance with all fields. Is this just a deep copy? confused. Need some explanation for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
默认的
Object.clone()
确实是一个浅拷贝。但是,除非您的对象实现了Cloneable
,否则它会抛出CloneNotSupportedException
。当您实现
Cloneable
时,您应该重写clone()
,通过调用clone()< /code> 所有本身可克隆的字段。
The default
Object.clone()
is indeed a shallow copy. However, it's designed to throw aCloneNotSupportedException
unless your object implementsCloneable
.And when you implement
Cloneable
, you should overrideclone()
to make it do a deep copy, by callingclone()
on all fields that are themselves cloneable.它是浅复制,因为它只复制对其他对象的引用。假设我们有这些类:
现在我们克隆 A 的实例:
firstA 和 secondaryA 中的 B 实例将是相同的。您不会拥有 B 实例的副本。这就是为什么clone()被称为浅拷贝。
您链接的页面上的图表应该可以帮助您理解所有这些。
It is a shallow copy because it only copies reference to other objects. Say we have these classes :
And now we make a clone of an instance of A :
The B instance in firstA and secondA will be the same. You won't have a copy of the B instance. This is why clone() is said to do shallow copy.
The diagrams on the page you linked should help you understand all that.
顺便说一句,我很惊讶没有人提到Joshua Bloch 对 Cloneable 的看法
As an aside, I'm surprised nobody has mentioned Joshua Bloch's views on Cloneable
clone() 创建所有字段的副本。
Java 具有原始类型和引用 - 当您克隆对象时,您会得到一个新对象,其中包含所有原始字段的副本(就像深层复制),而且您还拥有所有引用字段的副本。因此,结果你得到两个对象,它们拥有基元的副本和对相同对象的引用的副本 - 原始对象和复制的对象都将使用相同的对象。
clone() creates copy of all fields.
Java have primitive types and refences - when you clone your object you get a new object with copies of all primitive field (it is like deep copy) but also you have copy of all refernce fields. So in result you get two objects with they own copies of primitives and copies of references to the same objects - both original and copied object will use the same objects.
有些对象不提供深层复制。例如,ArrayList 将克隆列表,但不会克隆列表中的元素。以下内容来自 JavaDoc< /a> 对于 ArrayList:
Some objects do not provide a deep copy. For example, an ArrayList will clone the list, but not the elements in the list. The following is from the JavaDoc for ArrayList:
Object.clone() 的默认实现是浅拷贝。此行为对于具有大量原始字段或不可变字段的类型仍然有用。您可以查看如何正确重写克隆方法?了解如何正确重写它。调用 super.clone() 并转换生成的对象后,您可以根据需要克隆得更深。
隐含地,随着类型中复杂、可变字段数量的增加,克隆的价值就会减少。
The default implementation of Object.clone() is a shallow copy. This behavior is still useful for types that have a large number of primitive fields or Immutable fields. You can look at How to properly override clone method? for how to properly override it. After calling super.clone(), then casting the resulting object, you can then clone deeper as needed.
Implicitly, the value of clone diminishes as the number of complex, mutable fields on your type increases.
clone
的作用是为每个选择支持克隆的对象定义的。 Object.clone 是受保护的,因此任何对象都不允许克隆,除非有人专门定义了它。What
clone
does is defined for each object that chooses to support clone. Object.clone is protected, so no object allows clone unless someone has specifically defined it.是的。
但首先你需要你的类实现 Cloneable 并抛出异常
输出: true
Yes.
But first you need that your class will implement Cloneable and throw exception
Output: true