文章目录
Java 中内置了一些很有用的接口, Clonable 就是其中之一。
Object 类中存在一个 clone 方法,调用这个方法可以创建一个对象的 “拷贝”,但是要想合法调用 clone 方法,必须要先实现 Clonable 接口,否则就会抛出 CloneNotSupportedException 异常。
Cloneable 是一个空接口,作用是:表示当前对象是可以被克隆的。
class Student implements Cloneable {
public String name;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
'}';
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class Test {
public static void main(String[] args) throws CloneNotSupportedException {
Student student1 = new Student();
Student student2 = (Student) student1.clone();
System.out.println(student1);
System.out.println(student2);
System.out.println(student1 == student2); // false
}
}
【浅拷贝 VS 深拷贝】:
Cloneable 拷贝出的对象是一份 “浅拷贝”。
浅拷贝: 将原对象/原数组的引用直接赋给新对象/新数组,新对象/新数组只是原对象/原数组的一个引用。(修改新对象/新数组会改变原对象/原数组);
深拷贝: 创建一个新对象/新数组,将原对象/原数组的各项属性的“值”(数组的所有元素)拷贝过来,是“值”而不是“引用”。(修改新对象/新数组不会改变原对象/原数组)
class Money {
public double money = 9.9;
}
class Student implements Cloneable {
public String name;
public Money m = new Money();
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", m=" + m +
'}';
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class Test {
public static void main(String[] args) throws CloneNotSupportedException {
Student student1 = new Student();
Student student2 = (Student) student1.clone();
System.out.println("student修改前的结果:");
System.out.println("student1: " + student1.m.money);
System.out.println("student2: " + student2.m.money);
System.out.println("student修改后的结果:");
student2.m.money = 99.9;
System.out.println("student1: " + student1.m.money);
System.out.println("student2: " + student2.m.money);
}
}
// 执行结果
student修改前的结果:
student1: 9.9
student2: 9.9
student修改后的结果:
student1: 99.9
student2: 99.9
如上代码,我们可以看到,通过clone,我们只是拷贝了 Student 对象,但是 Student 对象中的 Money 对象并没有拷贝。通过 student2 这个引用修改了 money 的值后,student2 这个引用访问 money 的时候,值也发生了改变。这里就是发生了浅拷贝。那如何实现深拷贝呢?
class Money implements Cloneable {
public double money = 9.9;
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class Student implements Cloneable {
public String name;
public Money m = new Money();
@Override
public String toString() {
return "Animal{" +
"name='" + name + '\'' +
'}';
}
@Override
protected Object clone() throws CloneNotSupportedException {
// return super.clone();
Student student = (Student) super.clone();
student.m = (Money) this.m.clone();
return student;
}
}
public class Test {
public static void main(String[] args) throws CloneNotSupportedException {
Student student1 = new Student();
Student student2 = (Student) student1.clone();
System.out.println("student修改前的结果:");
System.out.println("student1: " + student1.m.money);
System.out.println("student2: " + student2.m.money);
System.out.println("student修改后的结果:");
student2.m.money = 99.9;
System.out.println("student1: " + student1.m.money);
System.out.println("student2: " + student2.m.money);
}
}
// 执行结果
student修改前的结果:
student1: 9.9
student2: 9.9
student修改后的结果:
student1: 9.9
student2: 99.9