On the realization of cloneable

Source: Internet
Author: User
Tags shallow copy

Today we see the prototype model, and then I learned the Java cloneable interface, this thing is a tag interface, there is nothing. I'm looking for the Clone method in object, which is declared as the propected type, but new in class B has an object of Class A a,a but cannot call the A.clone () method. While object B in class B can call the Clone method B.clone (); At first I did not understand what was going on, and later I thought a A, a, a, a, a, a, a, but a different father, objecta and A is not in a package, and it is a parent class, Then of course you can only use subclasses in Objecta A that have no overridden methods.

Then go down, although there is a clone () method in object, but using this method directly in Class A will cause a clonenotsupportedexception error. Looking directly at the API, it basically means this: the Clone () method in object is to implement a specific copy operation, such as an array (since the array has already implemented this interface), other classes need to implement the Cloneable interface if they want to invoke the Clone () method.

And finally, shallow copy and deep copy, in Java, so to understand, shallow copy is to copy a reference, and the deep copy is to copy the reference refers to the object , obviously, we want the Clone () method to achieve the effect of deep copy, Because copying references obviously doesn't have to be so hard.

First, let's take a look at shallow copy, where we have three classes, a test class, a Class A, and an index class. The specific code is as follows:

A class has a Name property of type string, plus a property of the Index object type.

 Public classAImplementscloneable{String name;    Index b;  Public intGetIndex () {returnB.getindex (); }     PublicIndex Index () {returnb; }     Public voidSetindex (Index b) { This. B =b; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicA Clone ()throwsclonenotsupportedexception{returnASuper. Clone (); }}

There are methods and properties in the index class that set the index size

 Public class Index {    publicint  getindex () {        return  Index;    }      Public void setindex (int  index) {        this. Index = index;    }      int index;}

Test class:

 Public classTest { Public Static voidMain (string[] args)throwsClonenotsupportedexception {A A=NewA (); A.setname (A); Index index=NewIndex (); Index.setindex (0);        A.setindex (index); System.out.println (A.getname ());//ASystem.out.println (A.getindex ());//0System.out.println ("A copy of a"); A b=A.clone (); System.out.println ("Modify properties prior to B"); System.out.println (B.getname ());//ASystem.out.println (B.getindex ());//0B.setname ("B"); Index.setindex (999);        B.setindex (index); System.out.println ("Modify properties after B"); System.out.println (B.getname ());//BSystem.out.println (B.getindex ());//999System.out.println ("View properties in a object after modifying B"); System.out.println (A.getname ());//ASystem.out.println (A.getindex ());//999System.out.println (B.index (). Equals (A.index ()));//true    }}

The above is a shallow copy, when copied and the wood has completely copied the object content once, the object of the reference type property is only copied reference.

To a deep copy: The Cloneable interface is also implemented in index and is added in the Clone () method of Class A:

 Public throws         clonenotsupportedexception{= B.clone ();         return Super . Clone ();     // the same test results are: a0 copy of a modified B before the property a0 after modifying B Property B999 after modifying B view the property in a objecta 0False

Therefore, if we want to clone the objects in the copied object there is a reference to the existence of the time, we must pay attention to the problem of deep replication.

On the realization of cloneable

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.