您的位置:

Java对象拷贝实现方法详解

一、浅拷贝与深拷贝

在Java中,对象拷贝可以分为浅拷贝和深拷贝。浅拷贝即拷贝对象的引用地址,使得指向同一块内存地址,而深拷贝则是在内存中重新申请一块与源对象完全相同的内存空间,使得复制对象和原始对象互不影响。

public class Person implements Cloneable {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    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;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

public class Test {
    public static void main(String[] args) throws CloneNotSupportedException {
        // 浅拷贝
        Person person1 = new Person("Tom", 18);
        Person person2 = person1;
        person1.setAge(20);
        System.out.println(person1.getAge());   //20
        System.out.println(person2.getAge());   //20

        // 深拷贝
        Person person3 = new Person("Jerry", 22);
        Person person4 = (Person) person3.clone();
        person3.setAge(25);
        System.out.println(person3.getAge());   //25
        System.out.println(person4.getAge());   //22
    }
}

二、利用Cloneable接口实现浅拷贝

Java中的Cloneable接口提供了对象的浅拷贝机制,只需要在对象上实现Cloneable接口并重写Object类中的clone方法即可。需要注意的是,与Serializable不同,Cloneable并不是只是用为标识用,而是真正地在底层实现了对象的浅拷贝。

public class Person implements Cloneable {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    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;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

public class Test {
    public static void main(String[] args) throws CloneNotSupportedException {
        Person person1 = new Person("Tom", 18);
        Person person2 = (Person) person1.clone();
        person1.setAge(20);
        System.out.println(person1.getAge());   //20
        System.out.println(person2.getAge());   //18
    }
}

三、利用序列化实现深拷贝

与浅拷贝不同的是,深拷贝需要重新申请一块完全相同的内存空间,在Java中,可以通过序列化来实现。

public class Person implements Serializable {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    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 Person deepClone() throws IOException, ClassNotFoundException {
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        ObjectOutputStream objOut = new ObjectOutputStream(byteOut);
        objOut.writeObject(this);

        ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
        ObjectInputStream objIn = new ObjectInputStream(byteIn);
        return (Person) objIn.readObject();
    }
}

public class Test {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Person person1 = new Person("Tom", 18);
        Person person2 = person1.deepClone();
        person1.setAge(20);
        System.out.println(person1.getAge());   //20
        System.out.println(person2.getAge());   //18
    }
}

四、第三方库实现深拷贝

除了利用序列化之外,Java中也有一些第三方库可以方便地实现深拷贝。

public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    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 static  T deepClone(T src) {
        return SerializationUtils.clone(src);
    }
}

public class Test {
    public static void main(String[] args) {
        Person person1 = new Person("Tom", 18);
        Person person2 = Person.deepClone(person1);
        person1.setAge(20);
        System.out.println(person1.getAge());   //20
        System.out.println(person2.getAge());   //18
    }
}

  

五、结论

对象拷贝可以分为浅拷贝和深拷贝两种方式,浅拷贝不会创建新的对象实例,而是指向同一个对象实例;深拷贝会创建一个新的对象实例,并且将原对象中所有的属性都复制到新对象中。实现对象拷贝有多种方式,可以利用Cloneable接口和重写clone方法来实现浅拷贝,可以利用序列化来实现深拷贝,也可以使用第三方库来方便地实现深拷贝。