浅拷贝:使用一个已知实例对新创建实例的成员变量逐个赋值,这个方式被称为浅拷贝。
深拷贝:不仅只是对成员变量赋值,而且如果成员变量是引用类型的话,也一并赋值引用类型,然后将新引用拷贝到该成员变量
假设我们新建一个类Person
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
以往,如果我们要复制的时候,直接如下:
public static void main(String[] args) throws Exception {
Person person1 = new Person("shengfan", 20);
Person person2 = person1; // 浅拷贝
System.out.println("person1:" + person1.toString());
System.out.println("person2:" + person2.toString());
}
输出:
可以看出,person2的内容和person1一样,复制成功。但是这种浅拷贝有个缺点就是如果person1变化了,则复制出来的也会跟着变,如下测试:
public static void main(String[] args) throws Exception {
Person person1 = new Person("shengfan", 20);
Person person2 = person1;
System.out.println("person1:" + person1.toString());
System.out.println("person2:" + person2.toString());
person1.setAge(100); // 只是改变person1的年龄
System.out.println("person1:" + person1.toString());
System.out.println("person2:" + person2.toString());
}
image.png
虽然上面只是修改了person1的年龄,但是person2却也跟着变了,因为主要是person1和person2指向的是堆中的同一个对象,如下所示:
image.png
因此,当修改person1的值后,person2也跟着改变。而深拷贝就是为了让person2完全独立出来,和person1无关联,不会因为person1的改变而改变,如下:
image.png
深拷贝的实现
Person需要实现Cloneable接口
public class Person implements Cloneable {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public Person clone() {
Person obj = null;
try {
obj = (Person) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return obj;
}
}
public static void main(String[] args) throws Exception {
Person person1 = new Person("shengfan", 20);
Person person2 = person1.clone();
System.out.println("person1:" + person1.toString());
System.out.println("person2:" + person2.toString());
person1.setAge(100);
System.out.println("person1:" + person1.toString());
System.out.println("person2:" + person2.toString());
}
执行结果:
image.png
完毕。。。
但是一般对象不会像我们这么简单,假设person对象里面还有一个cat对象
public class Cat {
private String name;
public Cat(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Person implements Cloneable {
private String name;
private int age;
private Cat cat;
public Person(String name, int age, Cat cat) {
this.name = name;
this.age = age;
this.cat = cat;
}
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age + ", cat=" + this.cat.getName() +
'}';
}
public Person clone() {
Person obj = null;
try {
obj = (Person) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return obj;
}
}
然后运行:
public static void main(String[] args) throws Exception {
Cat cat = new Cat("花花");
Person person1 = new Person("shengfan", 20, cat);
Person person2 = person1.clone();
System.out.println("person1:" + person1.toString());
System.out.println("person2:" + person2.toString());
cat.setName("笨妞");
person1.setCat(cat);
System.out.println("person1:" + person1.toString());
System.out.println("person2:" + person2.toString());
}
image.png
结果发现,虽然用了clone方法,然而person2里面的cat还是随着person1的修改跟着变化,
原因在于虽然person2是重新new出来的对象,和person1不是一个引用,然后person2里面的cat的引用依然和person1的cat引用一致。可以输出他们的cat在内存中的地址验证一下:
public static void main(String[] args) throws Exception {
Cat cat = new Cat("花花");
Person person1 = new Person("shengfan", 20, cat);
Person person2 = person1.clone();
cat.setName("笨妞");
person1.setCat(cat);
System.out.println("person1:" + person1.toString());
System.out.println("person2:" + person2.toString());
System.out.println("person1:" + System.identityHashCode(person1));
System.out.println("person2:" + System.identityHashCode(person2));
System.out.println("person1的cat:" + System.identityHashCode(person1.getCat()));
System.out.println("person2的cat:" + System.identityHashCode(person2.getCat()));
}
运行结果:
image.png
可以看出,虽然person1和person2并非同一个引用,但是里面的cat却是同一个,所以这边如果对象的成员也是一个引用对象的时候,需要成员也实现Cloneable接口,并且本身自己的clone需要对成员进行clone:
public class Cat implements Cloneable {
private String name;
public Cat(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Cat clone() {
Cat cat = null;
try {
cat = (Cat) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return cat;
}
}
public class Person implements Cloneable {
private String name;
private int age;
private Cat cat;
public Person(String name, int age, Cat cat) {
this.name = name;
this.age = age;
this.cat = cat;
}
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age + ", cat=" + this.cat.getName() +
'}';
}
public Person clone() {
Person obj = null;
try {
obj = (Person) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
obj.setCat(cat.clone()); // 此处需要对引用成员变量clone
return obj;
}
}
运行测试:
public static void main(String[] args) throws Exception {
Cat cat = new Cat("花花");
Person person1 = new Person("shengfan", 20, cat);
Person person2 = person1.clone();
System.out.println("person1:" + person1.toString());
System.out.println("person2:" + person2.toString());
cat.setName("笨妞");
person1.setCat(cat);
System.out.println("person1:" + person1.toString());
System.out.println("person2:" + person2.toString());
System.out.println("person1:" + System.identityHashCode(person1));
System.out.println("person2:" + System.identityHashCode(person2));
System.out.println("person1的cat:" + System.identityHashCode(person1.getCat()));
System.out.println("person2的cat:" + System.identityHashCode(person2.getCat()));
}
结果:
image.png
网友评论