The object class is the parent class of all classes, arrays, and enumeration classes, and is the root class of the class hierarchy. Each class uses object as a superclass. All objects, including
arrays) Implement this class of methods.
we saw that the Clone () method was modified with native to show that the efficiency of the native method is generally higher than the non-native method in Java . The second uses
Protected access permissions to decorate, that is, you want to apply the clone () method must inherit object, in Java all classes inherit the object class by default,
so you canignored.
Zui Then we look at the role of the Clone () method, which is stated in the official documentation:
By convention, the returned object should be obtained by calling Super.clone . If a class and all of its superclass (exceptObject ) follow this approximately
X.clone (). GetClass () = = X.getclass ().
By convention, the object returned by this method should be independent of the object (the object being copied). To get this independence, return the object in super.clone
Before, it is necessary to modify one or more fields of the object. This usually means that you want to copy the internal "deep structure" that contains the object being copied.
All mutable objects, and replaces references to those objects with references to the replicas. If a class contains only a basic field or a reference to an immutable object, that
You typically do not need to modify the fields in the object returned by Super.clone .
The clone method of the Object class performs a specific copy operation. First, if the class of this object does not implement interface cloneable, it throws
clonenotsupportedexception. Note that all arrays are considered to implement the interface cloneable. Otherwise, this method creates a class for this object.
A new instance, and all fields of the object are initialized strictly using the contents of the corresponding field of this object, as is the case with the assignment, and the contents of these fields are not
Be self-replicating. Therefore, this method performs a "shallow copy" of the object, not a "deep copy" operation.
The object class itself does not implement interface cloneable, so calling the clone method on an object that is class object will cause an exception to be thrown at run time.
To summarize the above, we can conclude that:
1, realize cloneable interface.
2. Overload clone () method.
3, implement the Clone () method by Super.clone (); Call the object implementation of the Clone () method to get a copy of the object.
Then let's look at two ways to clone the Clone () method:
Cloned objects:
public class Cloneobject implements Serializable{private string name;p ublic string GetName () {return name;} public void SetName (String name) {this.name = name;}}
(1) Shallow cloning:
public class Simpleclone implements Cloneable, Serializable {public cloneobject cloneobject = Null;public Simpleclon E (Cloneobject cloneobject) {this.cloneobject = Cloneobject;} Public Object Clone () {Simpleclone Newsimpleclone = null;try {newsimpleclone = (Simpleclone) super.clone ();} catch ( Clonenotsupportedexception e) {e.printstacktrace ();} return newsimpleclone;}}
Shallow cloning test:
public class Testclone {public static void main (string[] arg) {Cloneobject obj1 = new Cloneobject (); Obj1.setname ("Cloneone "); Cloneobject obj2 = new Cloneobject (); Obj2.setname ("Clonetwo"); Simpleclone simpleClone1 = new Simpleclone (OBJ1); Simpleclone simpleClone2 = new Simpleclone (obj2); simpleClone2 = (Simpleclone) simpleclone1.clone (); SimpleClone2.cloneObject.setName ("Clonethree"); System.out.println (SimpleClone1.cloneObject.getName ()); System.out.println (SimpleClone2.cloneObject.getName ());}}
Test results:
Clonethree
Clonethree
The above program creates two objects, obj1 and Obj2, and assigns "Cloneone" and "Clonetwo" to the name, respectively, and assigns values to two defined
A class that implements a shallow clone. We see the above program cloning for SIMPLECLONE1 and assigning the cloned object to SimpleClone2, and then
Re-assign the value to the Simpleclone. Finally we look at the output results and find that SimpleClone1.cloneObject.getName is along with
SimpleClone2 changes, meaning that a shallow clone simply clones all field values of the object and does not reference the field value of the reference type
object is cloned, and actually the cloned object points to the same block memory address.
(2) Deep cloning:
public class Depthclone {public final static object Objectcopy (object oldobj) {Object newObj = null;try {BYTEARRAYOUTP Utstream bo = new Bytearrayoutputstream (); ObjectOutputStream oo = new ObjectOutputStream (bo); Oo.writeobject (OLDOBJ); Bytearrayinputstream bi = new Bytearrayinputstream (Bo.tobytearray ()); ObjectInputStream oi= new ObjectInputStream (BI); NEWOBJ = Oi.readobject ();} catch (IOException e) {e.printstacktrace ();} catch (ClassNotFoundException e) {e.printstacktrace ();} return NEWOBJ;}}
Deep cloning test:
public class Testclone {public static void main (string[] arg) {Cloneobject obj1 = new Cloneobject (); Obj1.setname ("Cloneone "); Cloneobject obj2 = new Cloneobject (); Obj2.setname ("Clonetwo"); Simpleclone simpleClone1 = new Simpleclone (OBJ1); Simpleclone simpleClone2 = new Simpleclone (obj2); simpleClone2 = (Simpleclone) depthclone.objectcopy (SIMPLECLONE1); SimpleClone2.cloneObject.setName ("Clonethree"); System.out.println (SimpleClone1.cloneObject.getName ()); System.out.println (SimpleClone2.cloneObject.getName ());}}
Test results:
Cloneone
Clonethree
The above program through the Java object serialization mechanism, the object's state into a byte stream, and then through these values regenerated into a new object, and finally we
Found that SimpleClone1.cloneObject.getName is not changed with simpleClone2 change, that is to say simpleClone1
And SimpleClone2 are present in two different memory. When using Java serialization, it is worth noting that if this class is changed, then
Recompile, then this is an exception when deserializing the object just now, which can be resolved by adding the Serialversionuid property.