java中的clone()
是浅拷贝吗?
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").
我是从 wikipedia 读到的.
不明白为什么是浅拷贝。 clone()
将创建一个包含所有字段的新实例。这只是一个深拷贝吗?使困惑。需要我解释一下。
最佳答案
默认的 Object.clone()
确实是一个浅拷贝。但是,它旨在抛出 CloneNotSupportedException
,除非您的对象实现了 Cloneable
。
当您实现 Cloneable
时,您应该通过调用 clone 覆盖
在本身可克隆的所有字段上。clone()
以使其进行深度复制()
关于java - java中的clone()是浅拷贝吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5279256/