关于 java 中可克隆接口和 object.clone() 的混淆
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1067383/
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
Confusion about cloneable interface and object.clone() in java
提问by ambertch
If I have:
如果我有:
class foo implements Cloneable
and then do:
然后做:
bar = new foo();
bar.clone();
I get a shallow copy without needing to write any bar.clone()
code like I normally would need to do when I implement an interface.
我获得了一个浅拷贝,而无需bar.clone()
像实现接口时通常需要的那样编写任何代码。
My understanding is that an interface's functions must be filled in by the class implementing it, and Object.clone()
has no implementation (as per the docs, "The class Object does not itself implement the interface Cloneable")
我的理解是接口的函数必须由实现它的类填充,并且Object.clone()
没有实现(根据文档,“类 Object 本身不实现接口 Cloneable”)
So where does my shallow clone come from? Where is the code that implements bar.clone()
if Object.clone()
has no implementation? I'm confused.
那么我的浅克隆是从哪里来的呢?bar.clone()
如果Object.clone()
没有实现,实现的代码在哪里?我糊涂了。
采纳答案by Tom
Be very carefulusing clone. In fact, I would avoid it completely. I have neverneeded it. BUT... that being said, the best discussion of the topic I have ever read is by Joshua Bloch, in Effective Java. Read Item 11: "Override clone judiciously".
使用克隆要非常小心。事实上,我会完全避免它。我从来不需要它。但是……话虽如此,我读过的关于该主题的最佳讨论是由 Joshua Bloch 在 Effective Java 中撰写的。阅读第 11 条:“明智地覆盖克隆”。
PLEASE do yourself a favor and read that item. I actually recommend reading that entire chapter (and the rest of the book). Everything you need to know about clone and why I caution you about it is in there.
请帮自己一个忙并阅读该项目。我实际上建议阅读整章(以及本书的其余部分)。你需要知道的关于克隆的一切以及我为什么要提醒你关于它的一切都在那里。
Hope this helps.
希望这可以帮助。
回答by Cambium
Object.clone() has an implementation:
Object.clone() 有一个实现:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html#clone()
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html#clone()
This link explains the Cloneable interface: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Cloneable.html
此链接解释了 Cloneable 接口:http: //java.sun.com/j2se/1.5.0/docs/api/java/lang/Cloneable.html
An object must implement the Cloneable interface in order to call the clone() method, otherwise, it throws a CloneNotSupportedException.
对象必须实现 Cloneable 接口才能调用 clone() 方法,否则会抛出 CloneNotSupportedException。
By definition, all classes in Java extend the base Object class, and Object class has a default clone() method, even though Object itself does not implement Cloneable. The Object class's clone() method will be called if you do not override it yourself.
根据定义,Java 中的所有类都扩展了基础 Object 类,并且 Object 类具有默认的 clone() 方法,即使 Object 本身没有实现 Cloneable。如果您不自己覆盖 Object 类的 clone() 方法,它将被调用。
回答by newacct
If I have: "class foo implements cloneable"
and then do: bar = new foo(); bar.clone();
I get a shallow copy without needing to write any bar.clone() code like I normally would need to do when I implement an interface.
如果我有:“类 foo 实现可克隆”
然后做: bar = new foo(); bar.clone();
我得到了一个浅拷贝,而无需像实现接口时通常需要的那样编写任何 bar.clone() 代码。
That would only work if you are calling it within the class "foo", because the .clone() method inherited from Object
is protected.
这仅在您在类“foo”中调用它时才有效,因为继承自的 .clone() 方法Object
是受保护的。
My understanding is that an interface's functions must be filled in by the class implementing it, and Object.clone() has no implementation (as per the docs, "The class Object does not itself implement the interface Cloneable")
我的理解是接口的函数必须由实现它的类填充,并且 Object.clone() 没有实现(根据文档,“类 Object 本身不实现接口 Cloneable”)
(1) Object.clone()
does have an implementation. It makes a shallow copy of the object if the object implements Cloneable
. (2) The .clone()
method is not part of any interface. (3) Having a .clone()
method and implementing the Cloneable
interface are completely separate things. You only need to implement the Cloneable
interface if you intend to make use of Object
's clone
method; however, this is the recommended way to write a clone
method for your class -- to get its copy from the superclass's clone
method, which eventually goes up to Object
's clone
method.
(1)Object.clone()
确实有一个实现。如果对象实现 ,它会制作对象的浅拷贝Cloneable
。(2) 该.clone()
方法不属于任何接口。(3) 拥有.clone()
方法和实现Cloneable
接口是完全分开的事情。Cloneable
如果您打算使用Object
'sclone
方法,则只需要实现该接口;但是,这是clone
为您的类编写方法的推荐方法——从超类的clone
方法中获取它的副本,最终上升到Object
的clone
方法。
回答by amarnath harish
My understanding is that an interface's functions must be filled in by the class implementing it, and Object.clone() has no implementation (as per the docs, "The class Object does not itself implement the interface Cloneable")
我的理解是接口的函数必须由实现它的类填充,并且 Object.clone() 没有实现(根据文档,“类 Object 本身不实现接口 Cloneable”)
there is a difference between saying Object.clone() has no implementation and The class Object does not itself implement the interface Cloneable
说Object.clone() 没有实现和类 Object 本身不实现接口 Cloneable是有区别的
Object's clone method does have implementation, it does memory-copy of the object who called clone method.
对象的 clone 方法确实有实现,它对调用 clone 方法的对象进行内存复制。
you are right, Object class does not implement cloneable, all it does is check the object is cloneable or not .
你是对的,Object 类没有实现可克隆,它所做的只是检查对象是否可克隆。
the above answer point's you to read some book, i think i can give a quick solution so to answer your question
上面的回答点是你看书,我想我可以给出一个快速的解决方案来回答你的问题
So where does my shallow clone come from?Object's clone method
那么我的浅克隆是从哪里来的呢?对象的克隆方法
Where is the code that implements bar.clone() if Object.clone() has no implementation?it has implementation, written in native code.
如果 Object.clone() 没有实现,那么实现 bar.clone() 的代码在哪里?它有用本机代码编写的实现。