Java深浅拷贝(对象)

深浅拷贝(对象)

Cloneable:CloneNotSupportedException

  • 只有子类实现了Cloneable接口后才可以使用Object类提供的clone方法。
protected native Object clone() throws CloneNotSupportedException;
  • 要想让对象具有拷贝的功能,必须实现Cloneable接口(标识接口,表示此类允许被clone),并且在类中自定义clone调用Object类提供的继承权限clone方法

  • 浅拷贝:对象值拷贝对于浅拷贝而言,拷贝出来的对象仍然保留原对象的所有引用

    • 问题:牵一发而动全身只要任意一个拷贝对象(或原对象)中的引用发生改变,所有对象均会受到影响。
  • 深拷贝:深拷贝,拷贝出来的对象产生了所有引用的新的对象


如何实现深拷贝

  1. 包含的其他类继续实现Cloneable接口,并且调用clone方法(递归实现Clone)
  2. 使用序列化(*****)使用序列化进行深拷贝时,无须再实现Cloneable接口,只需要实现Serializable即可。
import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Teacher implements Serializable {
    private String name;
    private String job;

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

    public String getName() {
        return name;
    }

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

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }
}

class Student implements Serializable{

    private String name;
    private int age;
    private Teacher teacher;

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

    public Student cloneObject() throws Exception{
        ByteOutputStream bos = new ByteOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(this);
        ByteArrayInputStream bis = new ByteArrayInputStream(bos.getBytes());
        ObjectInputStream ois = new ObjectInputStream(bis);
        return (Student) ois.readObject();
    }

    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 Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

}
public class Test {
    public static void main(String[] args) throws Exception{
        Teacher teacher = new Teacher("小明","Java Teacher");
        Student student = new Student("小李",18,teacher);
        Student studentClone = student.cloneObject();
        System.out.println(student);
        System.out.println(studentClone);
        System.out.println("=================");
        System.out.println(studentClone.getName());
        System.out.println(studentClone.getAge());
        System.out.println(studentClone.getTeacher().getName());
        System.out.println(studentClone.getTeacher().getJob());
        System.out.println("------------------------------");
        System.out.println(teacher == studentClone.getTeacher());
    }
}

代码输出

tong.day2.Student@135fbaa4
tong.day2.Student@6acbcfc0
=================
小李
18
小明
Java Teacher
------------------------------
false

你可能感兴趣的:(编程)