Java中的浅复制与深复制

Java中的浅复制与深复制

clone、Cloneable和Serialiable

什么是浅复制?什么是深复制?两者有何区别?要弄清楚这些问题,一切都要从Object的一个方法clone方法和两个标记接口Cloneable和Serializable谈起!

首先来看看clone方法,clone方法是Java中根父类Object的一个方法,定义如下:

protected native Object clone() throws CloneNotSupportedException;

标记接口Cloneable表示实现这个接口的类是可以clone的,Serializable表示实现这个接口的类是可以序列化的,所谓序列化就是这个对象可以通过读写字节码的方式在网络上或各个系统间传输!clone方法的意思就是克隆出一个对象并返回,可以看到Object中clone方法并没有具体的实现,关键字native表示clone方法是个原生方法,方法的具体实现不在这个文件中,而是在其他编程语言实现的文件中,比如C或C++。

通过实现Cloneable接口实现对象的克隆

代码实现:

package com.pt.copy;

public class CloneDemo {
    public static void main(String[] args) {
        Student tom = new Student("tom", 18);
        School test = new School("test", tom);
        School clone = test.clone();
        System.out.println(clone == test);//false
        System.out.println(clone.getStudent() == test.getStudent());//true
        clone.getStudent().setName("alice");
        System.out.println(test.getStudent().getName()); 
        // alice 克隆对象修改数据,原对象也会改变
    }
}

class Student {
    String name;
    int age;

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

    public String getName() {
        return name;
    }

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

class School implements Cloneable{
    String name;
    Student student;

    public School(String name, Student student) {
        this.name = name;
        this.student = student;
    }

    public String getName() {
        return name;
    }

    public Student getStudent() {
        return student;
    }

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

    @Override
    public School clone() {
        try {
            // TODO: copy mutable state here, so the clone can't change the internals of the original
            return (School) super.clone();
        } catch (CloneNotSupportedException e) {
            throw new AssertionError();
        }
    }
}

由上述代码可以得出,通过实现clone方法得到的对象是原对象的浅复制,复制对象的属性引用与原对象的属性引用指向同一个地址。

通过实现Serialiable接口实现对象的序列化
package com.pt.copy;

import java.io.*;

public class SerializableDemo {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Student tom = new Student("tom", 18);
        School test = new School("test", tom);
        //写入流
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(test);
        //从流中读取
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bais);
        School  copy = (School) ois.readObject();
        oos.close();
        ois.close();
        baos.close();
        bais.close();
        System.out.println(copy == test); // false
        System.out.println(copy.getStudent() == test.getStudent()); //false
    }
}

class Student implements Serializable{
    String name;
    int age;

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

    public String getName() {
        return name;
    }

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

class School implements Cloneable, Serializable {
    String name;
    Student student;

    public School(String name, Student student) {
        this.name = name;
        this.student = student;
    }

    public String getName() {
        return name;
    }

    public Student getStudent() {
        return student;
    }

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

    @Override
    public School clone() {
        try {
            // TODO: copy mutable state here, so the clone can't change the internals of the original
            return (School) super.clone();
        } catch (CloneNotSupportedException e) {
            throw new AssertionError();
        }
    }
}

通过序列化实现对象的复制为深复制,对象的属性也复制了一份。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值