Java浅克隆与深克隆实现
一、为什么克隆
克隆的对象可能包含一些已经修改过的属性,保留着你想克隆对象的值,而new出来的对象的属性全是一个新的对象,对应的属性没有值,我们还要重新给这个对象的各个赋值。那么当需要一个新的对象来保存当前对象的“状态”就只能通过克隆了。有人会问,我把这个对象的临时属性一个一个的赋值给我新new的对象不也行嘛?的确能实现,但是就太麻烦了,另外,通过源码能够发现clone是一个native方法,在底层实现的,速度上肯定很快。
二、怎么实现克隆
主要有如下步骤:
1、对象的类实现Cloneable接口;
2、覆盖Object类的clone()方法 (覆盖clone()方法,访问修饰符设为public,默认是protected);
3、在clone()方法中调用super.clone()。
三、两种不同的克隆方法,浅克隆(ShallowClone)和深克隆(DeepClone)。
浅克隆是指拷贝对象时仅仅拷贝对象本身(包括对象中的基本变量),而不拷贝对象包含的引用指向的对象。
深克隆不仅拷贝对象本身,而且拷贝对象包含的引用指向的所有对象。
举例来说更加清楚:
浅克隆
package org.com.huang.test;
public class Student implements Cloneable {
private int age;
private String name;
public Student(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student [age=" + age + ", name=" + name + "]";
}
@Override
public Object clone() throws CloneNotSupportedException {
// TODO Auto-generated method stub
return super.clone();
}
public static void main(String[] args) throws CloneNotSupportedException {
Student student1 = new Student(20, "张三");
Student student2 = (Student) student1.clone();
student2.setAge(22);// 注意修改student2的age值 但是没有影响 student1的值
System.out.println("student1:" + student1.getName() + "-->"+ student1.getAge());
System.out.println("student2:" + student2.getName() + "-->"+ student2.getAge());
}
}
运行结果:
student1:张三-->20
student2:张三-->22
问题:引入克隆导致的问题
package org.com.huang.test;
class Teacher implements Cloneable {
private String name;
private Student student;
public String getName() {
return name;
}
public void setName(String name) {
t his.name = name;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
@Override
public String toString() {
return "Teacher [name=" + name + ", student=" + student + "]";
}
@Override
public Object clone() throws CloneNotSupportedException {
// TODO Auto-generated method stub
return super.clone();
}
public static void main(String[] args) throws CloneNotSupportedException {
Student s1 = new Student();
s1.setAge(20);
s1.setName("张三");
Teacher teacher1 = new Teacher();
teacher1.setName("小赵老师");
teacher1.setStudent(s1);
//为什么会出现以下结果, Teacher中的clone方法
Teacher teacher2 = (Teacher)teacher1.clone();
Student s2 = teacher2.getStudent();
s2.setName("李四");
s2.setAge(30);
System.out.println("teacher1:"+teacher1);
System.out.println("teacher2:"+teacher2);
}
}
运行结果:
teacher1:Teacher [name=小赵老师, student=Student [age=30, name=李四]]
teacher2:Teacher [name=小赵老师, student=Student [age=30, name=李四]
深克隆
要克隆的类和类中所有非基本数据类型的属性对应的类(注意clone()方法)
package org.com.huang.test;
class Teacher implements Cloneable {
private String name;
private Student student;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
@Override
public String toString() {
return "Teacher [name=" + name + ", student=" + student + "]";
}
@Override
public Object clone() throws CloneNotSupportedException {
// TODO Auto-generated method stub
//注意以下代码
Teacher teacher = (Teacher)super.clone();
teacher.setStudent((Student)teacher.getStudent().clone());
return teacher;
}
public static void main(String[] args) throws CloneNotSupportedException {
Student s1 = new Student();
s1.setAge(20);
s1.setName("张三");
Teacher teacher1 = new Teacher();
teacher1.setName("小赵老师");
teacher1.setStudent(s1);
Teacher teacher2 = (Teacher)teacher1.clone();
teacher2.setName("小明老师");
Student s2 = teacher2.getStudent();
s2.setName("李四");
s2.setAge(30);
System.out.println("teacher1:"+teacher1);
System.out.println("teacher2:"+teacher2);
}
}
运行结果:
teacher1:Teacher [name=小赵老师, student=Student [age=20, name=张三]]
teacher2:Teacher [name=小明老师, student=Student [age=30, name=李四]]