浅拷贝
浅拷贝仅仅复制所考虑的对象,而不复制它所引用的对象
package Pattern;
public class Solution {
public static void main(String[] args) throws CloneNotSupportedException {
Teacher teacher = new Teacher();
teacher.setName("老师");
teacher.setAge(40);
Student student1 = new Student();
student1.setName("学生1");
student1.setAge(18);
student1.setTeacher(teacher);
Student student2 = (Student) student1.clone();
System.out.println("拷贝后");
System.out.println(student2.getName());
System.out.println(student2.getAge());
System.out.println(student2.getTeacher().getName());
System.out.println(student2.getTeacher().getAge());
System.out.println("更改学生1年纪---------------");
student1.setAge(30);
System.out.println(student1.getAge());
System.out.println(student2.getAge());
System.out.println("修改老师的信息后-------------");
teacher.setName("新老师");
System.out.println(student1.getTeacher().getName());
System.out.println(student2.getTeacher().getName());
}
}
class Teacher implements Cloneable {
private String name;
private int age;
...
get,set
...
}
class Student implements Cloneable {
private String name;
private int age;
private Teacher teacher;
...
get,set
...
public Object clone() throws CloneNotSupportedException {
// 浅拷贝
return super.clone();
}
}
输出结果
拷贝后
学生1
18
老师
40
更改学生1年纪---------------
30
18
修改老师的信息后-------------
新老师
新老师
两个引用student1和student2指向不同的两个对象,但是两个引用student1和student2中的两个teacher引用指向的是同一个对象,所以说明是浅拷贝。

深拷贝
- 深拷贝把要复制的对象所引用的对象都复制了一遍。
- 深拷贝是一个整个独立的对象拷贝,深拷贝会拷贝所有的属性,并拷贝属性指向的动态分配的内存。当对象和它所引用的对象一起拷贝时即发生深拷贝。深拷贝相比于浅拷贝速度较慢并且花销较大。
public class Solution {
public static void main(String[] args) throws CloneNotSupportedException {
Teacher teacher = new Teacher();
teacher.setName("老师");
teacher.setAge(40);
Student student1 = new Student();
student1.setName("学生1");
student1.setAge(18);
student1.setTeacher(teacher);
Student student2 = (Student) student1.clone();
System.out.println("拷贝后");
System.out.println(student2.getName());
System.out.println(student2.getAge());
System.out.println(student2.getTeacher().getName());
System.out.println(student2.getTeacher().getAge());
System.out.println("更改学生1年纪---------------");
student1.setAge(30);
System.out.println(student1.getAge());
System.out.println(student2.getAge());
System.out.println("修改老师的信息后-------------");
teacher.setName("新老师");
System.out.println(student1.getTeacher().getName());
System.out.println(student2.getTeacher().getName());
}
}
class Teacher implements Cloneable {
private String name;
private int age;
...
get,set
...
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class Student implements Cloneable {
private String name;
private int age;
private Teacher teacher;
...
get,set
...
public Object clone() throws CloneNotSupportedException {
// 浅拷贝
// return super.clone();
// 深拷贝:
Student student = (Student) super.clone();
// 将Teacher对象复制一份并重新set进来
student.setTeacher((Teacher) student.getTeacher().clone());
return student;
}
}
}
结果
拷贝后
学生1
18
老师
40
更改学生1年纪---------------
30
18
修改老师的信息后-------------
新老师
老师
两个引用student1和student2指向不同的两个对象,两个引用student1和student2中的两个teacher引用指向的是两个对象,但对teacher对象的修改只能影响student1对象,所以说是深拷贝。

网友评论