java对象复制-深拷贝和浅拷贝
前言
今天在对ArrayList进行复制的时候,发现复制后的List中的对象的属性发生改变后,原数组也会发生改变,经过一番检索后总结出一些结论。
在日常开发中,对象的复制是非常常见的,而实际上,复制类型也是有区分的,主要有深复制和浅复制。
浅复制
对基本数据类型进行值传递,对引用数据类型进行引用传递般的拷贝.如图为浅拷贝的核心:
实现方式
通过java中的clone方法
通过clone方式实现浅复制需要,它的实现必须实现 Cloneable
接口,否者将抛出 CloneNotSupportedException
这个异常。
先给定两个实体类
public class Student1 implements Cloneable{
Age age;
String name;
public Age getAge() {
return age;
}
public void setAge(Age age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public String toString() {
return "Student1{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
public class Age {
int num;
public Age(int num) {
this.num = num;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
@Override
public String toString() {
return "Age{" +
"num=" + num +
'}';
}
}
再看测试方法:
public class Test {
public static void main(String ...args) throws CloneNotSupportedException {
Student1 student1 = new Student1();
Age age = new Age(21);
student1.setAge(age);
student1.setName("小明");
//1.通过clone()克隆
Student1 student2 = (Student1) student1.clone();
System.out.println(student1);
System.out.println(student2);
//2.修改student1
student1.getAge().setNum(22);
System.out.println(student1);
System.out.println(student2);
//3.查看hash
System.out.println(student1.getAge().hashCode());
System.out.println(student2.getAge().hashCode());
}
}
结果如图:
从结果可以看到,第一步通过调用clone()方法后,创建一个和student1一样的新对象,但这只是浅拷贝,这从第二步就可以看出,当改变student1的属性时,student2也根本改变,而且通过hash值也能看出对象属性的hash是一致的。
通过构造函数实现复制
先给定一个实体类
public class Student2 {
Age age;
String name;
public Student2(Age age, String name) {
this.age = age;
this.name = name;
}
Student2(Student2 p2){
this.age = p2.age;
this.name = p2.name;
}
public Age getAge() {
return age;
}
public void setAge(Age age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student1{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
public class Age {
int num;
public Age(int num) {
this.num = num;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
@Override
public String toString() {
return "Age{" +
"num=" + num +
'}';
}
}
再给定测试类
public static void main(String ...args) {
Age age = new Age(21);
Student2 student1 = new Student2(age,"小明");
//构造函数
Student2 student2 = new Student2(student1);
System.out.println(student1);
System.out.println(student2);
//修改student1
student1.getAge().setNum(22);
System.out.println(student1);
System.out.println(student2);
//查看对象属性的hash
System.out.println(student1.getAge().hashCode());
System.out.println(student2.getAge().hashCode());
}
结果如图:
同样可以看到与clone对象一样的结果。
深复制
深复制一样有两种方式实现:
- 序列化(serialization)这个对象,再反序列化回来,就可以得到这个新的对象,无非就是序列化的规则需要我们自己来写。
- 继续利用 clone() 方法,既然 clone() 方法,是我们来重写的,实际上我们可以对其内的引用类型的变量,再进行一次 clone()。
继续改写Age类,让其实现Cloneable接口,同时改写Student1的clone方法。
public class Age implements Cloneable{
String name;
int num;
public Age(int num) {
this.num = num;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
@Override
public String toString() {
return "Age{" +
"num=" + num +
'}';
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class Student1 implements Cloneable{
Age age;
String name;
public Age getAge() {
return age;
}
public void setAge(Age age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public Object clone() throws CloneNotSupportedException {
Student1 clone = (Student1) super.clone();
clone.age = (Age) this.age.clone();
return clone;
}
@Override
public String toString() {
return "Student1{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
测试方法:
public class Test {
public static void main(String ...args) throws CloneNotSupportedException {
Student1 student1 = new Student1();
Age age = new Age(21);
student1.setAge(age);
student1.setName("小明");
//1.通过clone()克隆
Student1 student2 = (Student1) student1.clone();
System.out.println(student1);
System.out.println(student2);
//2.修改student1
student1.getAge().setNum(22);
System.out.println(student1);
System.out.println(student2);
//3.查看hash
System.out.println(student1.getAge().hashCode());
System.out.println(student2.getAge().hashCode());
}
}
结果:
可以看到,这次在改变student1的时候,student2不会跟着改变。hash值也不一样了。