java copy --clone

Outline:

  1. How java copy of the object.
  2. Shallow copy
  3. Deep copy

 

A, java how to copy objects

Person p = new Person();
Person p2 = p;

The example is not a copy operation, just assigned to the referenced object p p2,2 variables point to the same address a heap.

I want to achieve the copy operation needs to do two things:

(1) implement the Cloneable interface, which is an empty interface without rewriting any method.

public interface Cloneable {}

(2) a method override clone, clone is noted that native method in the class Object, not Cloneable interface.

 

Second, shallow copy

Rewrite the Person class

@Data
public class Person implements Cloneable {
    private String name;
    private int age;
    private Date birth;

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", birth=" + birth.getTime() +
                '}';
    }

    @Override
    public Person clone() {
        Object clone = null;
        try {
            clone = super.clone();
        } catch (CloneNotSupportedException e) {
        }
        return (Person)clone;
    }
}

Call clone method to copy objects:

Person p1 = new Person();
Person p2 = p1.clone();

So p1, p2 is not an object of.

 

Third, the deep copy

Person object member variables above are native java objects and basic data types, light enough to make copies of copies Person has a function.

If the member variable for the Person plus a custom class

@Data
public class Father {
    private String name;
    private int age;

    @Override
    public String toString() {
        return "Father{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
public class Person implements Cloneable {
    private String name;
    private int age;
    private Date birth;
    private Father father;
}

father of the target person to do so copies can not be achieved

Test cases:

public static void main(String[] args){
        Person p1 = new Person();
        p1.setName("p1");
        Father f1 = new Father();
        f1.setName("f1");
        p1.setFather(f1);
        Person p2 = (Person) p1.clone();
        p2.getFather().setName("f2");
        p2.setName("p2");
        System.out.println(p1);
        System.out.println(p2);

        //结果
        //Person{name='p1', age=0, birth=null, father=Father{name='f2', age=0}}
        //Person{name='p2', age=0, birth=null, father=Father{name='f2', age=0}}
    }

You can see the person object implements the copy, but the father still objects refer to the same object.

 

Deep copy implemented steps of:

(1) that the class member variable custom implementation Cloneable interface and override the clone method.

@Data
public class Father implements Cloneable{
    private String name;
    private int age;

    @Override
    public String toString() {
        return "Father{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public Father clone() {
        Object clone = null;
        try {
            clone = super.clone();
        } catch (CloneNotSupportedException e) {
        }
        return (Father) clone;
    }
}

(2) The method of member variables override clone, also you need to copy the object member variable explicit assignment.

@Data
public class Person implements Cloneable {
    private String name;
    private int age;
    private Date birth;
    private Father father;

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", birth=" + birth +
                ", father=" + father +
                '}';
    }

    @Override
    public Person clone() {
        Person clone = Null ;
         the try { 
            clone = (the Person) Super .clone (); 
        } the catch (CloneNotSupportedException E) { 
        } 
        // Here explicit assignment 
        clone.setFather (father.clone ());
         return clone; 
    } 
}

At this point the person completed copy father variables, the above examples of useful test to test.

public static void main(String[] args){
    Person p1 = new Person();
    p1.setName("p1");
    Father f1 = new Father();
    f1.setName("f1");
    p1.setFather(f1);
    Person p2 = (Person) p1.clone();
    p2.getFather().setName("f2");
    p2.setName("p2");
    System.out.println(p1);
    System.out.println(p2);
    //结果
    //Person{name='p1', age=0, birth=null, father=Father{name='f1', age=0}}
    //Person{name='p2', age=0, birth=null, father=Father{name='f2', age=0}}
}

 

Guess you like

Origin www.cnblogs.com/liuboyuan/p/11309077.html