java 浅拷贝中的 clone() 是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5279256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is clone() in java shallow copy?
提问by Josh Morrison
Is clone()
in java a shallow copy?
是clone()
Java中的浅拷贝?
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").
最终这会到达 Object(最上层的类)的 clone() 方法,该方法创建与对象相同类的新实例,并将所有字段复制到新实例(“浅拷贝”)。
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.
我不明白为什么它是一个浅拷贝。clone()
将创建一个包含所有字段的新实例。这只是一个深拷贝吗?使困惑。需要给我一些解释。
回答by Chris Jester-Young
The default Object.clone()
is indeed a shallow copy. However, it's designed to throw a CloneNotSupportedException
unless your object implements Cloneable
.
默认Object.clone()
确实是浅拷贝。但是,它旨在抛出 a ,CloneNotSupportedException
除非您的对象实现Cloneable
.
And when you implement Cloneable
, you shouldoverride clone()
to make it do a deep copy, by calling clone()
on all fields that are themselves cloneable.
并且当您实现 时Cloneable
,您应该clone()
通过调用clone()
本身可克隆的所有字段来覆盖以使其进行深度复制。
回答by krtek
It is a shallow copy because it only copies reference to other objects. Say we have these classes :
它是浅拷贝,因为它只拷贝对其他对象的引用。假设我们有这些类:
class A {
B variable
A() {
variable = new B();
}
}
class B { }
And now we make a clone of an instance of A :
现在我们克隆 A 的一个实例:
A firstA = new A();
A secondA = firstA.clone();
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.
firstA 和 secondA 中的 B 实例将相同。您将没有 B 实例的副本。这就是为什么 clone() 被称为执行浅拷贝的原因。
The diagrams on the page you linked should help you understand all that.
您链接的页面上的图表应该可以帮助您理解所有这些。
回答by Brian Agnew
As an aside, I'm surprised nobody has mentioned Joshua Bloch's views on Cloneable
顺便说一句,我很惊讶没有人提到Joshua Bloch 对 Cloneable 的看法
If you've read the item about cloning in my book, especially if you read between the lines, you will know that I think clone is deeply broken. There are a few design flaws, the biggest of which is that the Cloneable interface does not have a clone method. And that means it simply doesn't work: making something Cloneable doesn't say anything about what you can do with it. Instead, it says something about what it can do internally. It says that if by calling super.clone repeatedly it ends up calling Object's clone method, this method will return a field copy of the original.
如果你读过我书中关于克隆的文章,特别是如果你在字里行间阅读,你就会知道我认为克隆已经被深深地破坏了。有一些设计缺陷,其中最大的是 Cloneable 接口没有克隆方法。这意味着它根本不起作用:制作可克隆的东西并没有说明你可以用它做什么。相反,它说明了它可以在内部做什么。它说如果通过重复调用 super.clone 最终调用了 Object 的 clone 方法,该方法将返回原始字段的副本。
回答by Max Komarychev
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.
clone() 创建所有字段的副本。Java 有原始类型和引用——当你克隆你的对象时,你会得到一个带有所有原始字段副本的新对象(就像深拷贝),但你也有所有引用字段的副本。因此,结果您会得到两个对象,它们拥有原始对象的副本和对相同对象的引用副本——原始对象和复制对象都将使用相同的对象。
回答by jt.
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 JavaDocfor ArrayList:
某些对象不提供深拷贝。例如,ArrayList 将克隆列表,但不会克隆列表中的元素。以下来自ArrayList的JavaDoc:
public Object clone() Returns a shallow copy of this ArrayList instance. (The elements themselves are not copied.)
public Object clone() Returns a shallow copy of this ArrayList instance. (The elements themselves are not copied.)
回答by Dilum Ranatunga
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.
Object.clone() 的默认实现是浅拷贝。此行为对于具有大量原始字段或不可变字段的类型仍然很有用。您可以查看如何正确覆盖克隆方法?了解如何正确覆盖它。在调用 super.clone() 之后,然后投射生成的对象,然后您可以根据需要进行更深入的克隆。
Implicitly, the value of clone diminishes as the number of complex, mutable fields on your type increases.
隐含地,随着类型上复杂、可变字段数量的增加, clone 的价值会减少。
回答by bmargulies
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.
什么clone
是为每个选择支持克隆的对象定义的。Object.clone 是受保护的,因此任何对象都不允许克隆,除非有人专门定义了它。
回答by Vladp
Yes.
是的。
But first you need that your class will implement Cloneable and throw exception
但首先你需要你的类将实现 Cloneable 并抛出异常
class A implements Cloneable{
public int y;
public B b;
public A(){
b = new B();
}
public static void main(String[] args) throws CloneNotSupportedException{
A a = new A();
A a2 = (A) a.clone();
System.out.print(a.b==a2.b);
}
}
Output: true
输出:真