java深克隆和浅克隆

package com.mouse.test.clonedemo.two;

/**
 * Created by Mahone Wu on 2018/10/19.
 */
public class Dog implements Cloneable {

    private String type;

    private String color;

    private int age;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
package com.mouse.test.clonedemo.two;

/**
 * Created by Mahone Wu on 2018/10/19.
 */
public class Person implements Cloneable  {

    private String name;

    private int age;

    private Dog dog;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        //浅克隆
        Object object = super.clone();
        return object;

        //深克隆
        /*Person newPerson = (Person) super.clone();
        Dog dog = (Dog) newPerson.getDog().clone();
        newPerson.setDog(dog);
        return newPerson;*/

    }


    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("Person{");
        sb.append("name='").append(name).append('\'');
        sb.append(", age=").append(age);
        sb.append(", dog=").append(dog);
        sb.append('}');
        return sb.toString();
    }
}
package com.mouse.test.clonedemo.two;

/**
 * Created by Mahone Wu on 2018/10/19.
 */
public class Main {

    public static void main(String[] args)throws Exception {

        Person person = new Person();
        person.setAge(22);
        person.setName("test");

        Dog dog = new Dog();
        dog.setAge(1);
        dog.setColor("黄色");
        dog.setType("x dog");

        person.setDog(dog);

        Person clonePerson = (Person) person.clone();
        clonePerson.setAge(1);
        clonePerson.setName("new person");

        System.out.println(person);
        System.out.println(clonePerson);

    }
}


java深克隆和浅克隆_第1张图片
image.png


java深克隆和浅克隆_第2张图片
image.png

深克隆的另外一种方式

package com.mouse.test.clonedemo.four;

import java.io.*;

/**
 * Created by Mahone Wu on 2018/10/19.
 */
public class Person  implements Serializable{

    private String name;

    private int age;

    private Dog dog;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    protected Object deepCopy()   {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(this);

            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bis);
            return ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("Person{");
        sb.append("name='").append(name).append('\'');
        sb.append(", age=").append(age);
        sb.append(", dog=").append(dog);
        sb.append('}');
        return sb.toString();
    }
}
package com.mouse.test.clonedemo.four;

import java.io.Serializable;

/**
 * Created by Mahone Wu on 2018/10/19.
 */
public class Dog implements Serializable {

    private String type;

    private String color;

    private int age;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }


}

package com.mouse.test.clonedemo.four;

/**
 * Created by Mahone Wu on 2018/10/19.
 */
public class Main {
    public static void main(String[] args)throws Exception {
        Person person = new Person();
        person.setAge(22);
        person.setName("test-1");

        Dog dog = new Dog();
        dog.setAge(1);
        dog.setColor("黄色");
        dog.setType("x dog");

        person.setDog(dog);

        Person clonePerson = (Person) person.deepCopy();
        clonePerson.setAge(1);
        clonePerson.setName("new person");

        System.out.println(person);
        System.out.println(clonePerson);
    }
}
java深克隆和浅克隆_第3张图片
image.png

你可能感兴趣的:(java深克隆和浅克隆)