深拷贝和浅拷贝
浅拷贝,只是拷贝基本类型的数据,而引用类型数据,是复制的引用而不是复制的对象。
深拷贝,将原对象所有的数据复制一份到另一个新开辟的空间。
Java中的浅拷贝可以用实现Cloneable接口调用Object类中的clone方式来实现浅拷贝。
Java中的深拷贝可以通过实现Serializable接口实现(先使对象实现Serializable接口,然后把对象写到一个流里,再从流里读出来,便可以重建对象)。
下面有例子:我们定义两个学生类,一个使用浅拷贝,一个使用深拷贝。Student1是浅拷贝,实现Cloneable接口,Student2是深拷贝实现Serializable接口,两个学生类中的属性中都有一个老师类的属性。我们来看一下浅拷贝和深拷贝的区别吧。
Student1:
public class Student1 implements Cloneable{
private String name;
private Teacher teacher;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
public Object clone() throws CloneNotSupportedException{
//调object类的clone方法
return super.clone();
}
}
Student2:
private static final long serialVersionUID = 1L;
private String name;
private Teacher teacher;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
//深拷贝
public Object deepClone() throws ClassNotFoundException, IOException{
//序列化
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(this);
// 反序列化
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois= new ObjectInputStream(bis);
return ois.readObject();
}
}
Teacher类:
import java.io.Serializable;
//Teacher必须实现Serializable接口,否则Student2中写入到流时,会报错
public class Teacher implements Serializable{
private static final long serialVersionUID = 1L;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
测试类:
import java.io.IOException;
public class demoCopy {
public static void main(String[] args) {
Teacher teacher = new Teacher();
teacher.setName("张老师");
Student1 student1 = new Student1();
student1.setName("张三");
student1.setTeacher(teacher);
Student1 student11 = null;
//浅拷贝
try {
student11 = (Student1)student1.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
Student2 student2 = new Student2();
student2.setName("李四");
student2.setTeacher(teacher);
Student2 student22 = null;
//深拷贝
try {
student22 = (Student2) student2.deepClone();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//更改老师信息
teacher.setName("李老师");
System.out.println("student11的老师名字:"+student11.getTeacher().getName());
System.out.println("student22的老师名字:"+student22.getTeacher().getName());
}
}
控制台输出的结果是
我们可以看出浅拷贝只是将teacher对象的引用拷贝给了student11,所以当后面teacher的姓名信息更改时,会影响student11里面的老师信息。而student22是将student2中的teacher的对象的所有信息拷贝了一份到流中,而不是只拷贝了对象的引用,所以后面teacher对象更改姓名信息后,对student22不会影响。
我们对Student11类和Teacher类中进行如下更改后,也可以使Student11使用Cloneable接口后达到深拷贝一样的效果。
更改Student11中的clone方法:
public Object clone() throws CloneNotSupportedException{
//更改前
// return super.clone();
//更改后
Student1 student = (Student1)super.clone();
student.teacher = (Teacher) teacher.clone();
return student;
}
使Teacher同时继承Cloneable,添加clone方法
public Object clone() throws CloneNotSupportedException{
return super.clone();
}
其他的类不变,这时Student11也达到了深拷贝的效果。
下面是控制台输出的结果。