浅克隆(ShallowClone) 和深克隆(DeepClone)
浅克隆是指拷贝对象时仅仅拷贝对象本身(包括对象中的基本变量),而不拷贝对象包含的引用指向的对象。
深克隆不仅拷贝对象本身,而且拷贝对象包含的引用指向的所有对象。
Employee类
public class Employee implements Cloneable{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Employee(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Employee [name=" + name + ", age=" + age + "]";
}
public Employee clone() throws CloneNotSupportedException{
Employee cloned = (Employee) super.clone();
return cloned;
}
}
浅克隆演示
public class Person implements Cloneable{
private String name;
private Employee emp;
public Person(String name, Employee emp) {
super();
this.name = name;
this.emp = emp;
}
public Employee getEmp() {
return emp;
}
public void setEmp(Employee emp) {
this.emp = emp;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person [name=" + name + ", emp=" + emp + "]";
}
public Person clone() throws CloneNotSupportedException{
Person cloned = (Person) super.clone();
return cloned;
}
/**
* 浅克隆
* 不克隆类中的引用对象
* @param args
* @throws CloneNotSupportedException
*/
public static void main(String[] args) throws CloneNotSupportedException {
Person person = new Person("Tommy",new Employee("职员",19));
person.setName("Tommy");
//浅克隆
Person cloned = person.clone();
cloned.setName("Arthur");
//此时修改克隆对象的Employee相当于修改person的
cloned.getEmp().setName("克隆员工");
cloned.getEmp().setAge(32);
System.out.println("original = "+person);
System.out.println("copy = "+cloned);
}
}
结果:
original = Person [name=Tommy, emp=Employee [name=克隆员工, age=32]]
copy = Person [name=Arthur, emp=Employee [name=克隆员工, age=32]]
深克隆演示
public class Person2 implements Cloneable{
private String name;
private Employee emp;
public Employee getEmp() {
return emp;
}
public void setEmp(Employee emp) {
this.emp = emp;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Person2(String name, Employee emp) {
super();
this.name = name;
this.emp = emp;
}
@Override
public String toString() {
return "Person2 [name=" + name + ", emp=" + emp + "]";
}
public Person2 clone() throws CloneNotSupportedException{
Person2 cloned = (Person2) super.clone();
cloned.setEmp(cloned.getEmp().clone());
return cloned;
}
/**
* 深克隆
* 类中的引用对象也克隆
* @param args
* @throws CloneNotSupportedException
*/
public static void main(String[] args) throws CloneNotSupportedException {
Person2 person = new Person2("Tommy",new Employee("职员",19));
person.setName("Tommy");
//浅克隆
Person2 cloned = person.clone();
cloned.setName("Arthur");
//此时修改克隆对象的Employee不会修改person
cloned.getEmp().setName("克隆员工");
cloned.getEmp().setAge(32);
System.out.println("original = "+person);
System.out.println("copy = "+cloned);
}
}
结果:
original = Person2 [name=Tommy, emp=Employee [name=职员, age=19]]
copy = Person2 [name=Arthur, emp=Employee [name=克隆员工, age=32]]