一、深拷贝和浅拷贝
1.深拷贝
深拷贝是指源对象与拷贝对象互相独立,其中任何一个对象的改动都不会对另外一个对象造成影响。
也就是说,深拷贝把要复制的对象所引用的对象都复制了一遍。
2.浅拷贝
拷贝出来的目标对象的指针和源对象的指针指向的内存空间是同一块空间,浅拷贝只是一种简单的拷贝。
也就是说,浅拷贝仅仅复制所考虑的对象,而不复制它所引用的对象。
3.深拷贝和浅拷贝都是对象拷贝
二、具体实列
1.浅拷贝
public class Copy {
public static void main(String[] args) throws CloneNotSupportedException {
Master master = new Master();
master.setName("A");
master.setAge(20);
Dog dog1 = new Dog();
dog1.setName("DOG");
dog1.setAge(4);
dog1.setMaster(master);
Dog dog2 = (Dog) dog1.clone();
System.out.println("-------------拷贝后-------------");
System.out.println(dog2.getName());
System.out.println(dog2.getAge());
System.out.println("master:");
System.out.println(dog2.getMaster().getName());
System.out.println(dog2.getMaster().getAge());
System.out.println("-------------修改master的信息后-------------");
master.setName("B");
System.out.println("Dog1的master为: " + dog1.getMaster().getName());
System.out.println("Dog2的master为: " + dog2.getMaster().getName());
}
}
class Master 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;
}
}
class Dog implements Cloneable {
private String name;
private int age;
private Master master;
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 Master getMaster() {
return master;
}
public void setMaster(Master master) {
this.master = master;
}
public Object clone() throws CloneNotSupportedException {
Object object = super.clone();
return object;
}
}
运行结果:
-------------拷贝后-------------
DOG
4
master:
A
20
-------------修改master的信息后-------------
Dog1的master为: B
Dog2的master为: B
2.深拷贝
public class Copy {
public static void main(String[] args) throws CloneNotSupportedException {
Master master = new Master();
master.setName("A");
master.setAge(28);
Dog dog1 = new Dog();
dog1.setName("DOG");
dog1.setAge(4);
dog1.setMaster(master);
Dog dog2 = (Dog) dog1.clone();
System.out.println("-------------拷贝后-------------");
System.out.println(dog2.getName());
System.out.println(dog2.getAge());
System.out.println("master:");
System.out.println(dog2.getMaster().getName());
System.out.println(dog2.getMaster().getAge());
System.out.println("-------------修改master的信息后-------------");
master.setName("B");
System.out.println("dog1的master为: " + dog1.getMaster().getName());
System.out.println("dog2的master为: " + dog2.getMaster().getName());
}
}
class Master 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 Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class Dog implements Cloneable {
private String name;
private int age;
private Master master;
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 Master getMaster() {
return master;
}
public void setMaster(Master master) {
this.master = master;
}
public Object clone() throws CloneNotSupportedException {
Dog dog = (Dog) super.clone();
dog.setMaster((Master) dog.getMaster().clone());
return dog;
}
}
运行结果:
-------------拷贝后-------------
DOG
4
master:
A
28
-------------修改master的信息后-------------
dog1的master为: B
dog2的master为: A