clone()方法深浅克隆 与 数组copyof()的深浅拷贝
clone()方法
要实现clone()方法,对象所在类必须实现Cloneable接口。
虽然Cloneable接口里空无一物,但是Cloneable接口对实现它的类进行了标记 (否则调用clone()会报CloneNotSupportedException异常)
我们使用的clone()方法来自于lang包中的Object类,需要重写clone()方法
需要注意的是clone()方法默认的返回值类型Object,因为Object是所有类的父类,重写方法时返回值可以改成自己需要类型
public class Student implements Cloneable { //实现Cloneable接口
private String name;
private Integer age;
@Override
protected Student clone() throws CloneNotSupportedException {//覆写Object中clone()方法
Student student = (Student) super.clone(); //clone()方法返回的是Object类型需要转换
return student; //返回值类型改为Student
}
public Student(String name, Integer age) {
this.name = name;
this.age = age;
}
public Student() {
}
}
----浅克隆----
使用clone()方法并不是只是复制了原对象的引用,而是自动的在堆中开辟一块新的空间,并且复制了原对象的基本数据类型的属性值给到新对象
测试类中生成Student的实例:
public class TestClone {
public static void main(String[] args) {
Student student = new Student("张三", 18);
Student newStudent = null;
try { //捕获clone()抛出的异常
newStudent = student.clone();//克隆student赋值给newStudent,转换为Student类型
newStudent.setName("李四"); //如果此时改变新对象的属性原对象属性是否会改变呢?
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
System.out.println("newStudent"+newStudent.getName());//打印newStudent的name属性
System.out.println("student"+student.getName());//打印原student的name属性
}
}
结果:
newStudent李四
student张三
Process finished with exit code 0
但是这只能克隆基本数据类型的属性,对于引用类型类型却只能克隆引用(内存地址),也就是说会出现两个对象的属性不独立的情况。
----深克隆----
如果样本对象中的属性是引用类型时,我们要进行深层克隆,方法如下:
在引用类型属性的类中也实现Cloneable接口,并属性的类中重写clone()方法,在克隆类中的clone()方法中调用属性类中的clone()方法,然后赋值给需要返回的对象的属性,此时在操作拷贝对象时不影响原有的对象。两个对象的属性就不在是同一个引用了,而是相互独立的。
代码如下:(get,set,toString省略)
属性类
class Classes implements Cloneable{
private Integer classId; //班级号
public Classes(Integer classId) {
this.classId = classId;
}
@Override
protected Classes clone() throws CloneNotSupportedException {
return (Classes)super.clone(); //把返回值类型从Object改为Classes
}
}
克隆类
public class Student implements Cloneable {
private String name;
private Integer age;
private Classes classes;
@Override
protected Student clone() throws CloneNotSupportedException {
Student student = (Student) super.clone();
Classes classes = classes.clone(); //克隆引用类型的属性
student.setClasses(classes); //赋值
return student;
}
public Student(String name, Integer age, Classes classes) {
this.name = name;
this.age = age;
this.classes = classes;
}
}
测试类
public class TestClone {
public static void main(String[] args) {
Student student = new Student("张三", 18,new Classes(1));
Student newStudent = null;
try { //捕获clone()抛出的异常
newStudent =student.clone();//克隆student赋值给newStudent,转换为Student类型
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
System.out.println("——————————更改前");
System.out.println("newStudent的班级"+newStudent.getClasses().getClassId());//打印newStudent的班级信息
System.out.println("student的班级"+student.getClasses().getClassId());//打印原student的班级信息
newStudent.setClasses(new Classes(2)); //更改克隆后的引用类型属性
System.out.println("——————————更改后");
System.out.println("newStudent的班级"+newStudent.getClasses().getClassId());//打印newStudent的班级信息
System.out.println("student的班级"+student.getClasses().getClassId());//打印原student的班级信息
}
}
结果
——————————更改前
newStudent的班级1
student的班级1
——————————更改后
newStudent的班级2
student的班级1
Process finished with exit code 0
所以,克隆对象时,如果对象的属性是引用类型时,一定要进行深层克隆,防止出现两个对象的属性是相同引用的情况
----数组copyOf()----
数组的copyOf()方法和对象的克隆性质差不多
1.当数组元素的是基本数据类型时,会生成一个新的数组(开辟新的堆空间)并复制原数组的元素给新的数组。
2.当数组元素为引用类型时,copyOf()会生成一个新的数组并复制原数组的元素的引用给新的数组,这时操作新数组元素会影响原数组元素(两个数组元素的指针指向同一个地址)。