Java中深浅拷贝

一:浅拷贝:
只有子类实现Cloneable接口才可以使用Object使用的clone方法。

protected native Object clone() throws CloneNotSupportedException;

浅拷贝是将原对象的信息拷贝,拷贝后的对象和原对象指向同一块空间。



class Teacher {
    private String name;
    private String position;

    public Teacher(String name, String position) {
        this.name = name;
        this.position = position;
    }

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

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

class Student implements  Cloneable
{
    private String name;
    private Integer age;
    private Teacher teacher;

    public Student(String name, Integer age, Teacher teacher) {
        this.name = name;
        this.age = age;
        this.teacher = teacher;
    }

    @Override
    protected Student clone() throws CloneNotSupportedException {
        Student student=null;
        student=(Student)super.clone();
        return student;
    }

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

public class QianCopy
{
    public static void main(String[] args) throws CloneNotSupportedException {
        Teacher teacher=new Teacher("ali","manager");
        Student student=new Student("sophia",10,teacher);
        System.out.println("这是原student");
        System.out.println(student);
        System.out.println("这是cloneStudent");
        Student cloneStudent=student.clone();
        System.out.println(cloneStudent);

        System.out.println("修改techer姓名");
        teacher.setName("Jack");//修改teacher的名称,student里的teacher姓名会改变
        System.out.println("这是修改后原student");
        System.out.println(student);
        System.out.println("这是修改后cloneStudent");
        System.out.println(cloneStudent);
    }
}

在这里插入图片描述上述clone是浅拷贝,因为拷贝后的teacher和原teacher指向同一块空间,原teacher改变,clone后的student里的teacher也改变。

二:深拷贝
深拷贝指:原对象的修改不会影响拷贝后的对象。
利用序列化,因为序列化后:将二进制字节流内容写到一个媒介(文本或字节数组),然后是从这个媒介读取数据,原对象写入这个媒介后拷贝给clone对象,原对象的修改不会影响clone对象,因为clone对象是从这个媒介读取。

import java.io.*;

//深拷贝
class Teacher1 implements Serializable {
    private String name;
    private String position;

    public Teacher1(String name, String position) {
        this.name = name;
        this.position = position;
    }

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

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

class Student1 implements  Cloneable,Serializable
{
    private String name;
    private Integer age;
    private Teacher1 teacher;

    public Student1(String name, Integer age, Teacher1 teacher) {
        this.name = name;
        this.age = age;
        this.teacher = teacher;
    }

    public Student1 cloneSer () throws Exception {
        Student1 student=null;
        //在内存中创建一个字节数组缓冲区,所有发送到输出流的数据保存在该字节数组中
        //默认创建一个大小为32的缓冲区
        ByteArrayOutputStream byOut=new ByteArrayOutputStream();
        //对象的序列化输出
        ObjectOutputStream outputStream=new ObjectOutputStream(byOut);//通过字节数组的方式进行传输
        outputStream.writeObject(this);  //将当前student对象写入字节数组中

        //在内存中创建一个字节数组缓冲区,从输入流读取的数据保存在该字节数组缓冲区
        ByteArrayInputStream byIn=new ByteArrayInputStream(byOut.toByteArray()); //接收字节数组作为参数进行创建
        ObjectInputStream inputStream=new ObjectInputStream(byIn);
        student=(Student1)inputStream.readObject(); //从字节数组中读取
        return student;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", teacher=" + teacher +
                '}';
    }
}
public class ShenCopy {
    public static void main(String[] args) throws Exception {
        Teacher1 teacher=new Teacher1("ali","manager");
        Student1 student=new Student1("sophia",10,teacher);
        System.out.println("这是原student");
        System.out.println(student);
        System.out.println("这时cloneStudent");
        Student1 cloneStudent=student.cloneSer();
        System.out.println(cloneStudent);

        System.out.println("将teacher姓名进行修改");
        teacher.setName("Jack");
        System.out.println("这是修改后原student");
        System.out.println(student);
        System.out.println("这是修改后cloneStudent");
        System.out.println(cloneStudent);
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值