文章目录
- 前言
- Clonable 接口
- 克隆方法代码的实现
- 浅拷贝
- 深拷贝
- 总结
前言
Clonable 接口
克隆方法代码的实现
//1.当类要调用克隆方法时,这个类要实现一个Cloneable接口
class Student implements Cloneable{
public String name;
public int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
//2.类要重写克隆方法时,调用克隆方法
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class Test {
//3.当类要调用克隆方法时,要在main方法后面添加throws CloneNotSupportedException(声明异常)
public static void main(String[] args) throws CloneNotSupportedException{
Student student = new Student("zhangsan",10);
System.out.println(student);
//克隆一个跟Student一样的对象
Student student1 = (Student) student.clone();//4.返回值是Object,父类给子类会报错,要强转成子类,向下转型
System.out.println(student1);
}
}
总结:
1.当类要调用克隆方法时,这个类要implements Cloneable接口
2.在类里面要重写克隆方法
3.要在main方法后面添加 throws CloneNotSupportedException(声明异常)
4.实例化克隆对象时,因为重写方法的返回值是Object,父类给子类会报错,所以要强转成子类
浅拷贝
被克隆对象和克隆对象引用变量的内容是一样的,存储的地址也一样,当变量的值改变了,克隆对象和被克隆对象的引用指向的还是同一个。
所以浅拷贝就是,被克隆对象引用谁,克隆对象就引用谁。
类似于小时候大家基本都经历过的两个孩子看电视抢一个遥控器,孩子1要看哪个台,孩子2就必须看哪个台。
class Money{
public double m = 19.9;
}
class Student implements Cloneable{
public String name;
public int age;
//在Student类中实例化money,以便Student调用
Money money = new Money();
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class Test {
public static void main(String[] args) throws CloneNotSupportedException{
Student student = new Student("张三",10);
Student student1 = (Student) student.clone();
System.out.println("student "+student.money.m);
System.out.println("student1 "+student1.money.m);
System.out.println("==============");
student.money.m=99.9;
System.out.println("student "+student.money.m);
System.out.println("student1 "+student1.money.m);
}
}
解析图:
深拷贝
把被克隆对象引用的对象再克隆一份出来,让克隆的对象引用。
当被克隆对象引用的对象改变了,克隆的对象引用的对象还是原来的值。
就是两个孩子看电视一人一个遥控器,孩子1转台不会影响孩子2
class Money implements Cloneable{
public double m = 19.9;
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class Student implements Cloneable{
public String name;
public int age;
//在Student类中实例化money,以便Student调用
Money money = new Money();
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
protected Object clone() throws CloneNotSupportedException {
Student tmp = (Student)super.clone();
tmp.money = (Money) this.money.clone();
return tmp;
}
}
public class Test {
public static void main(String[] args) throws CloneNotSupportedException{
Student student = new Student("张三",10);
Student student1 = (Student) student.clone();
System.out.println("student "+student.money.m);
System.out.println("student1 "+student1.money.m);
System.out.println("==============");
student.money.m=99.9;
System.out.println("student "+student.money.m);
System.out.println("student1 "+student1.money.m);
}
解析图:
总结
再次学了浅拷贝和深拷贝,比上一次思路清晰多了,浅拷贝的原理和代码较容易理解,深拷贝的原理和代码较复杂,理解得还不够深入,需多看几遍巩固。
今晚脑子有点乱,希望我也能轻舟已过万重山。