1.浅复制和深复制的概念
浅复制是指当复制某个对象时,仅仅复制该对象的基本数据类型,而不复制其引用数据类型;而深复制不仅复制其基本数据类型,而且复制其引用数据类型。无论浅复制还是深复制,在使用该功能之前对象都实现Cloneable接口,重写clone()方法。特别注意,复制对象和原对象不是同一个对象,但复制对象和原对象的类型是一样的。
2.实例代码
2.1 浅复制代码
Person类
public class Person implements Cloneable { // 姓名 private String name; // 年龄 private String age; // 汽车 private Car car; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", car=" + car + "]"; } public Object clone() throws CloneNotSupportedException { return super.clone(); } }
Car类
public class Car { // 车名 private String name; // 颜色 private String color; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } @Override public String toString() { return "Car [name=" + name + ", color=" + color + "]"; } }
测试类
public class ShadowCopyTest { public static void main(String[] args) { Person p1 = new Person(); p1.setName("Tom"); p1.setAge("25"); Car c1 = new Car(); c1.setName("BMW"); c1.setColor("Brown"); p1.setCar(c1); try { Person p2 = (Person) p1.clone(); Car c2 = p2.getCar(); c2.setName("BYD"); c2.setColor("Black"); p2.setCar(c2); System.out.println("p1: " + p1); System.out.println("p2: " + p2); } catch (CloneNotSupportedException e) { e.printStackTrace(); } } }
测试结果
2.2 深复制代码
Person类
public class Person implements Cloneable { // 姓名 private String name; // 年龄 private String age; // 汽车 private Car car; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", car=" + car + "]"; } public Object clone() throws CloneNotSupportedException { Person newPerson = (Person) super.clone(); newPerson.car = (Car) car.clone(); return newPerson; } }
Car类
public class Car implements Cloneable { // 车名 private String name; // 颜色 private String color; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } @Override public String toString() { return "Car [name=" + name + ", color=" + color + "]"; } public Object clone() throws CloneNotSupportedException { return super.clone(); } }
测试类
public class DeepCopyTest { public static void main(String[] args) { Person p1 = new Person(); p1.setName("Tom"); p1.setAge("25"); Car c1 = new Car(); c1.setName("BMW"); c1.setColor("Brown"); p1.setCar(c1); try { Person p2 = (Person) p1.clone(); Car c2 = p2.getCar(); c2.setName("BYD"); c2.setColor("Black"); p2.setCar(c2); System.out.println("p1: " + p1); System.out.println("p2: " + p2); } catch (CloneNotSupportedException e) { e.printStackTrace(); } } }测试结果