clone()方法学习

来源:互联网 发布:iphone软件怎么汉化 编辑:程序博客网 时间:2024/05/14 13:08

Object中有个方法:

protected native Object clone() throws CloneNotSupportedException;

对于类X,现在有类X的一个实例xInstance

1、X没有实现java.lang.Cloneable

xInstance.clone();会抛出异常CloneNotSupportedException

2、X实现了java.lang.Cloneable

如果X在clone() 中只是调用super.clone(),则会有一个X的新实例xNewInstance(注意:生成xNewInstance时并不会调用构函),xNewInstance中域的值和xInstance中域是的值完全一样的,即xNewInstance是xInstance的一个浅copy(shadow copy)

 

 

 

输出:

Father's constructor
Son's constructor
Father:2010-06-05 19:40:36-265
Father:9023134
Son:2010-06-05 19:40:38-265
Son:19336051
libing0, libing1, libing2, null, null, null, null, null, null, null,
arrayString:6336176
After 2 second, begin clone
Father:2010-06-05 19:40:36-265
Father:9023134
Son:2010-06-05 19:40:38-265
Son:19336051
libing0, libing1, libing2, null, null, null, null, null, null, libing9,
arrayString:6718604

原创粉丝点击