简体   繁体   English

关于java中的可克隆接口和object.clone()的困惑

[英]Confusion about cloneable interface and object.clone() in java

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()没有实现(根据文档,“类对象本身不实现接口Cloneable”)

So where does my shallow clone come from? 那么我的浅层克隆来自哪里? Where is the code that implements bar.clone() if Object.clone() has no implementation? 如果Object.clone()没有实现,那么实现bar.clone()的代码在哪里? I'm confused. 我糊涂了。

Be very careful using clone. 使用克隆要非常小心 In fact, I would avoid it completely. 事实上,我会完全避免它。 I have never needed it. 从来没有需要它。 BUT... that being said, the best discussion of the topic I have ever read is by Joshua Bloch, in Effective Java. 但是......话虽如此,我所读过的关于这个主题的最佳讨论是由Joshua Bloch撰写的,在Effective Java中。 Read Item 11: "Override clone judiciously". 阅读第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. 希望这可以帮助。

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. 根据定义,Java中的所有类都扩展了基类Object,而Object类具有默认的clone()方法,即使Object本身没有实现Cloneable。 The Object class's clone() method will be called if you do not override it yourself. 如果您不自己覆盖它,将调用Object类的clone()方法。

If I have: "class foo implements cloneable" 如果我有:“class foo实现cloneable”

and then do: bar = new foo(); 然后执行:bar = new foo(); bar.clone(); 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()代码,就像我通常在实现接口时需要做的那样。

That would only work if you are calling it within the class "foo", because the .clone() method inherited from Object is protected. 只有在类“foo”中调用它时才会有效,因为从Object继承的.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()没有实现(根据文档,“类对象本身不实现接口Cloneable”)

(1) Object.clone() does have an implementation. (1) Object.clone()确实有一个实现。 It makes a shallow copy of the object if the object implements Cloneable . 如果对象实现了Cloneable它会生成对象的浅表副本。 (2) The .clone() method is not part of any interface. (2) .clone()方法不是任何接口的一部分。 (3) Having a .clone() method and implementing the Cloneable interface are completely separate things. (3)使用.clone()方法并实现Cloneable接口是完全不同的事情。 You only need to implement the Cloneable interface if you intend to make use of Object 's clone method; 如果你打算使用Objectclone方法,你只需要实现Cloneable接口; 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. 但是,这是为您的类编写clone方法的推荐方法 - 从超类的clone方法获取其副本,该方法最终会转到Objectclone方法。

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()没有实现(根据文档,“类对象本身不实现接口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. Object的clone方法确实有实现,它对调用clone方法的对象进行内存复制。

you are right, Object class does not implement cloneable, all it does is check the object is cloneable or not . 你是对的,Object类不实现cloneable,它只是检查对象是否可克隆。

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? 如果Object.clone()没有实现,那么实现bar.clone()的代码在哪里? it has implementation, written in native code. 它有实现,用本机代码编写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM