问题
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.
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")
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.
回答1:
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. Read Item 11: "Override clone judiciously".
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.
回答2:
Object.clone() has an implementation:
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
An object must implement the Cloneable interface in order to call the clone() method, otherwise, it throws a 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.
回答3:
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.
That would only work if you are calling it within the class "foo", because the .clone() method inherited from Object
is protected.
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")
(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.
回答4:
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")
there is a difference between saying Object.clone() has no implementation and The class Object does not itself implement the interface Cloneable
Object's clone method does have implementation, it does memory-copy of the object who called clone method.
you are right, Object class does not implement cloneable, all it does is check the object is cloneable or not .
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.
来源:https://stackoverflow.com/questions/1067383/confusion-about-cloneable-interface-and-object-clone-in-java