网站首页> 文章专栏> Java深拷贝,浅拷贝
Java深拷贝,浅拷贝
深拷贝:深拷贝又称为深复制,深克隆。深拷贝不仅拷贝对象本身,而且还拷贝对象包含的引用所指向的对象,拷贝出来的对象的所有变量(不包含那些引用其他对象的变量)的值都含有与原来对象的相同的值,那些引用其他对象的变量将指向新复制出来的新对象,而不指向原来的对象,简单地说,深拷贝不仅拷贝对象,而且还拷贝对象包含的引用所指向的对象。
再简单的说就是浅拷贝只拷贝对象,不拷贝引用。深拷贝对象引用全拷贝
创建一个指向对象的引用变量的拷贝。
Student student = new Student("张三", 12);
Student copyStudent = student;
System.out.println(student);
System.out.println(copyStudent);
com.rjxy.copy.Student@1b6d3586
com.rjxy.copy.Student@1b6d3586
分析:从结果可以看出,它们输出的地址值是相同的,那么它们肯定是同一个对象。new Student("张三",12) 时在堆区创建了,而变量student和copyStudent只是引用而已,它们都指向了同一个对象。这就叫做引用拷贝。
创建对象本身的一个副本。
Student student = new Student("张三", 12);
Student cloneStudent = (Student) student.clone();
System.out.println(student);
System.out.println(cloneStudent);
输出结果
com.rjxy.copy.Student@1b6d3586
com.rjxy.copy.Student@4554617c
分析:由输出结果可以看出,它们的地址是不同的,也就是说创建了新的对象, 而不是把原对象的地址赋给了一个新的引用变量,这就叫做对象拷贝。
注:深拷贝和浅拷贝都是对象拷贝
class Teacher implements Cloneable {
String name;
public Teacher(String name) {
this.name = name;
}
@Override
public String toString() {
return "Teacher{" +
"name='" + name + '\'' +
'}';
}
}
Student类:
class Student implements Cloneable {
String name;
Integer id;
Teacher teacher;
public Student(String name, Integer id, Teacher teacher) {
this.name = name;
this.id = id;
this.teacher = teacher;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", id=" + id +
", teacher=" + teacher +
'}';
}
}
测试:
public class CopyTest { public static void main(String[] args) throws Exception { Teacher teacher = new Teacher("李四老师"); Student student = new Student("张三", 12, teacher); Student cloneStudent = (Student) student.clone(); System.out.println(student); System.out.println(cloneStudent); teacher.name = "王五老师"; System.out.println("=========修改老师信息后========="); System.out.println(student); System.out.println(cloneStudent); } } }
输出结果:
Student{name='张三', id=12, teacher=Teacher{name='李四老师'}}
Student{name='张三', id=12, teacher=Teacher{name='李四老师'}}
=========修改老师信息后=========
Student{name='张三', id=12, teacher=Teacher{name='王五老师'}}
Student{name='张三', id=12, teacher=Teacher{name='王五老师'}}
两个student和cloneStudent指向不同的对象,但是两个引用student和copyStudent中的teacher引用指向的是同一个对象,所以说是浅拷贝。
class Teacher implements Cloneable { String name; public Teacher(String name) { this.name = name; } @Override public String toString() { return "Teacher{" + "name='" + name + '\'' + '}'; } @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } }
Student类:
class Student implements Cloneable {
String name;
Integer id;
Teacher teacher;
public Student(String name, Integer id, Teacher teacher) {
this.name = name;
this.id = id;
this.teacher = teacher;
}
@Override
protected Object clone() throws CloneNotSupportedException {
// 浅复制时
// return super.clone();
//深复制时
Student student = (Student) super.clone();
student.teacher = (Teacher) student.teacher.clone();
return student;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", id=" + id +
", teacher=" + teacher +
'}';
}
}
测试代码:
Teacher teacher1 = new Teacher("李四老师");
Student student = new Student("张三", 12, teacher1);
Student cloneStudent = (Student) student.clone();
System.out.println(student);
System.out.println(cloneStudent);
teacher1.name = "王五老师";
System.out.println("=========修改老师信息后=========");
System.out.println(student);
System.out.println(cloneStudent);
结果:
Student{name='张三', id=12, teacher=Teacher{name='李四老师'}}
Student{name='张三', id=12, teacher=Teacher{name='李四老师'}}
=========修改老师信息后=========
Student{name='张三', id=12, teacher=Teacher{name='王五老师'}}
Student{name='张三', id=12, teacher=Teacher{name='李四老师'}}
修改名称前:
修改名称后:
构造函数实现:
Teacher teacher = new Teacher("李四老师"); Student student = new Student("张三", 12, teacher); Student cloneStudent = new Student("小二", 11, new Teacher(teacher.name)); System.out.println(student); System.out.println(cloneStudent); teacher.name = "王五老师"; System.out.println("=========修改老师信息后========="); System.out.println(student); System.out.println(cloneStudent);
序列化:
public Object deepClone() throws Exception {
// 序列化
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();
}
注:对象需要实现Serializable接口
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.1</version>
</dependency>
<dependency>
public Student deepClone2() {
return SerializationUtils.clone(this);
}
以上就是浅拷贝和深拷贝,看起来是不是很简单, 动手操作一下吧!
2024-05-21 23:18:32 回复