本文共 4251 字,大约阅读时间需要 14 分钟。
浅克隆 通常只是对克隆的实例进行复制,但里面的其他子对象,都是共用的。
深克隆 克隆的时候会克隆它的子对象的引用,里面所有的变量和子对象都是又额外拷贝了一份。
浅克隆
/**
* 浅克隆
* @author InJavaWeTrust
*
*/
class Father implements Cloneable {
public String name;
public int age;
public Father(String name, int age) {
this.name = name;
this.age = age;
}
}
public class Child implements Cloneable {
public String name;
public int age;
public Father father;
public Child(String name, int age, Father father) {
this.name = name;
this.age = age;
this.father = father;
}
public Object clone() {
Child child = null;
try {
child = (Child) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return child;
}
public static void main(String[] args) {
Father father = new Father("李刚", 44);
Child child1 = new Child("李启铭", 14, father);
Child child2 = (Child) child1.clone();
child2.father.name = "赵刚";
child2.father.age = 40;
child2.name = "赵启铭";
child2.age = 15;
System.out.println(child1.name + " " + child1.age);
System.out.println(child1.father.name + " " + child1.father.age);
System.out.println(child2.name + " " + child2.age);
System.out.println(child2.father.name + " " + child2.father.age);
}
//运行结果
//李启铭 14
//赵刚 40
//赵启铭 15
//赵刚 40
//浅克隆时father对象都是共用的,所以father重新赋值之后,之前的值也跟着变化。
}
深克隆
/**
* 深克隆
* @author InJavaWeTrust
*
*/
class Father implements Cloneable {
public String name;
public int age;
public Father(String name, int age) {
this.name = name;
this.age = age;
}
public Object clone() {
Father father = null;
try {
father = (Father) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return father;
}
}
public class Child implements Cloneable {
private String name;
private int age;
private Father father;
public Child(String name, int age, Father father) {
this.name = name;
this.age = age;
this.father = father;
}
public Object clone() {
Child child = null;
try {
child = (Child) super.clone();
child.father = (Father) father.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return child;
}
public static void main(String[] args) {
Father father = new Father("李刚", 44);
Child child1 = new Child("李启铭", 14, father);
Child child2 = (Child) child1.clone();
child2.father.name = "赵刚";
child2.father.age = 40;
child2.name = "赵启铭";
child2.age = 15;
System.out.println(child1.name + " " + child1.age);
System.out.println(child1.father.name + " " + child1.father.age);
System.out.println(child2.name + " " + child2.age);
System.out.println(child2.father.name + " " + child2.father.age);
}
//运行结果
//李启铭 14
//李刚 44
//赵启铭 15
//赵刚 40
//深克隆时father对象也 重新拷贝了一份,所以father重新赋值之后,之前的值不会发生变化。
}
利用序列化也可以实现深克隆。
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
* 序列化深克隆
* @author InJavaWeTrust
*
*/
class Father implements Serializable {
private static final long serialVersionUID = 5339188002945518918L;
public String name;
public int age;
public Father(String name, int age) {
this.name = name;
this.age = age;
}
}
public class Child implements Serializable{
private static final long serialVersionUID = 991407861345394819L;
private String name;
private int age;
private Father father;
public Child(String name, int age, Father father) {
this.name = name;
this.age = age;
this.father = father;
}
public Object deepClone() {
Object object = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(this);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
object = ois.readObject();
} catch (Exception e) {
e.printStackTrace();
}
return object;
}
public static void main(String[] args) {
Father father = new Father("李刚", 44);
Child child1 = new Child("李启铭", 14, father);
Child child2 = (Child) child1.deepClone();
child2.father.name = "赵刚";
child2.father.age = 40;
child2.name = "赵启铭";
child2.age = 15;
System.out.println(child1.name + " " + child1.age);
System.out.println(child1.father.name + " " + child1.father.age);
System.out.println(child2.name + " " + child2.age);
System.out.println(child2.father.name + " " + child2.father.age);
}
//运行结果
//李启铭 14
//李刚 44
//赵启铭 15
//赵刚 40
}
转载地址:http://fcyzo.baihongyu.com/