Clone () method Detailed

First, the principle and application of cloning

clone allocated on the heap memory, allocated memory and the source object (i.e., an object method call clone) the same, then the original object using the respective corresponding fields, filled with new object fields, after the filling is completed, the method returns clone, a new the same object is created, the same can publish the new object reference to the outside. If you want the object to be processed, want to retain the original data in the next operation, clone is very convenient.

Second, to achieve cloning

1, was cloned class must implement their own Cloneable interface, to indicate the Object.clone () method can be legally copied by instances of that class field. Cloneable interface is actually a marker interface, there is no interface methods.

2, covering the Object.clone () method.

Third, the shallow copy and deep copy

Shallow copy: when filling new object domain, a simple field assignment.

Deep copy: By convention, the object returned by this method should be independent of the object (positive copied object). To obtain this independence, in super.clone before returning the object, it is necessary to modify one or more fields of the object. This usually means that you want to copy contains internal objects being copied "deep structure" of all variable objects, and replace the reference to a copy of the references to those objects. If a class contains only the basic fields of the same object or a reference, it is usually not necessary to modify super.clone returned object field. (Simply put, the inside of the object is also the object of a clone, rather than a simple reference assignment)

( Variable object: Objects created after field values can be changed)

Shallow copy:

class Head {
    String s;
    void set(String s1) {
        s = s1;
    }
}
public class Person implements Cloneable{
    
    Head head;
    Person(Head head) {
        this.head = head;
    }
    protected Object clone() throws CloneNotSupportedException{
        return super.clone();
    }
    public static void main(String[] args) throws CloneNotSupportedException{
        Person p = new Person(new Head());
        Person p1 = (Person)p.clone();
        System.out.println("p == p1 " + (p == p1));
        System.out.println("p.head == p1.head " + (p.head == p1.head));
    }

}

 Output:

p == p1 false
p.head == p1.head true

Result analysis:

You can see from the results had cloned create a new Person object, but p.head and p1.head still point to the same object, that is to say p and p1 still there is a link, this is we do not want to see.

 

Deep copy:

class Head implements Cloneable{
    String s;
    void set(String s1) {
        s = s1;
    }
    protected Object clone() throws CloneNotSupportedException{
        return super.clone();
    }
}
public class Person implements Cloneable{
    
    Head head;
    Person(Head head) {
        this.head = head;
    }
    protected Object clone() throws CloneNotSupportedException{
        Person p = (Person)super.clone();
        p.head = (Head)head.clone();
        return p;
    }
    public static void main(String[] args) throws CloneNotSupportedException{
        Person p = new Person(new Head());
        Person p1 = (Person)p.clone();
        System.out.println("p == p1 " + (p == p1));
        System.out.println("p.head == p1.head " + (p.head == p1.head));
    }

}

 Output:

p == p1 false
p.head == p1.head false

Result Analysis: To achieve a deep copy of the object you want to copy internal objects.

Good text recommended: Detailed clone method in Java

Guess you like

Origin www.cnblogs.com/mgblogs/p/11519125.html