java中的clone小结

基本概念

浅克隆:创建一个新对象,新对象的属性和原来对象完全相同,对于非基本类型属性,仍指向原有属性所指向的对象的内存地址。

深克隆:创建一个新对象,属性中引用的其他对象也会被克隆,不再指向原有对象地址。

avatar

测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public static void main(String[] args) throws Exception{
// 创建父亲(LiLiu),儿子(LiWu),孙子(LiLiu)并关联
Son father = new Son();
father.setName("LiSi");
Son son = new Son();
son.setName("LiWu");
Son grandSon = new Son();
grandSon.setName("LiLiu");
father.setSon(son);
son.setSon(grandSon);
// 调用clone方法
Son fatherCopy = (Son) father.clone();
boolean flag1 = fatherCopy==father;
boolean flag2 = fatherCopy.getSon() == son;
boolean flag3 = fatherCopy.getSon().getSon() == grandSon;
// 比较克隆后的地址
System.out.println(flag1);// false
System.out.println(flag2);// true
System.out.println(flag3);// true
// 比较Name
flag1= fatherCopy.getName()==father.getName();
flag2 = fatherCopy.getSon().getName() == son.getName();
flag3 = fatherCopy.getSon().getSon().getName() == grandSon.getName();
System.out.println(flag1);// true
System.out.println(flag2);// true
System.out.println(flag3);// true

//将对象写到流里
ByteArrayOutputStream byteOut=new ByteArrayOutputStream();
ObjectOutputStream objOut=new ObjectOutputStream(byteOut);
objOut.writeObject(father);
//从流里读出来
ByteArrayInputStream byteIn=new ByteArrayInputStream(byteOut.toByteArray());
ObjectInputStream objInput=new ObjectInputStream(byteIn);
fatherCopy = (Son) objInput.readObject();
flag1= fatherCopy==father;
flag2 = fatherCopy.getSon() == son;
flag3 = fatherCopy.getSon().getSon() == grandSon;
System.out.println(flag1);// false
System.out.println(flag2);// false
System.out.println(flag3);// false

// 比较Name
flag1= fatherCopy.getName()==father.getName();
flag2 = fatherCopy.getSon().getName() == son.getName();
flag3 = fatherCopy.getSon().getSon().getName() == grandSon.getName();
System.out.println(flag1);// false
System.out.println(flag2);// false
System.out.println(flag3);// false
}

从上文代码及运行结果不难看出,如果对象实现Cloneable并重写clone方法不进行任何操作时,调用clone是进行的浅克隆。而使用对象流将对象写入流然后再读出是进行的深克隆。

工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class CloneUtils {

private static final Logger LOG = LoggerFactory.getLogger(CloneUtils.class);

private CloneUtils{}

public static <T extends Serializable> T clone(T obj){
T cloneObj = null;
ByteArrayOutputStream out = null;
ByteArrayInputStream in = null;

try{

out = new ByteArrayOutputStream();
ObjectOutputStream obs = new ObjectOutputStream(out);
obs.writeObject(obj);
obs.close();

ios = new ByteArrayInputStream(out.toByteArray());
ObjectInputStream ois = new ObjectInputStream(ios);
cloneObj = (T)ois.readObject();
ois.close();

}catch(Exception ex){
LOG.error("拷贝对象时发生异常", e1);
}finally{
try(){
if(null != out){
out.close();
}
if(null != in){
in.close();
}
}catch(Exception ex){
LOG.warn("关闭字节流时发生异常", e2);
}
}

}

}

参考文献

  1. https://www.cnblogs.com/liqiangchn/p/9465186.html