【面试】--java 基础Cloneable 最彻底的clone是什么?
来源:互联网 发布:淘宝代理免费网上加盟 编辑:程序博客网 时间:2024/06/03 23:54
1 默认数组类型 引用类型都是浅copy
The method {@code clone} for class {@code Object} performs a* specific cloning operation. First, if the class of this object does* not implement the interface {@code Cloneable}, then a* {@code CloneNotSupportedException} is thrown. Note that all arrays* are considered to implement the interface {@code Cloneable} and that* the return type of the {@code clone} method of an array type {@code T[]}* is {@code T[]} where T is any reference or primitive type.* Otherwise, this method creates a new instance of the class of this* object and initializes all its fields with exactly the contents of* the corresponding fields of this object, as if by assignment; the* contents of the fields are not themselves cloned. Thus, this method* performs a "shallow copy" of this object, not a "deep copy" operation.
2 代码实例
public class CloneTest implements Cloneable { int count; CloneTest next; List<Integer> list = Lists.newArrayList(); public CloneTest(int count) { list.add(count); this.count = count; if (count > 0) next = new CloneTest(count - 1); } void add() { count++; list.add(count); if (next != null) next.add(); } public String toString() { String s = String.valueOf(count) + " "; if (next != null) s += next.toString(); return s+ JSON.toJSONString(list); } // 深拷贝 public Object clone() { Object o = null; o = new CloneTest(count); return o; } //浅拷贝 @Override public Object clone() { Object o = null; try { o = super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return o; } public static void main(String[] args) { CloneTest t = new CloneTest(3); System.out.println("t=" + t); CloneTest t1 = (CloneTest) t.clone(); System.out.println("t1=" + t1); t.add(); System.out.println(t.next == t1.next); System.out.println("after added\nt t=" + t + "\nt1=" + t1); }}
3 结果
t=3 2 1 0 [0][1][2][3]
t1=3 2 1 0 [0][1][2][3]
true
after added
t t=4 3 2 1 [0,1][1,2][2,3][3,4]
t1=3 3 2 1 [0,1][1,2][2,3][3,4]
深拷贝
t=3 2 1 0 [0][1][2][3]
t1=3 2 1 0 [0][1][2][3]
false
after added
t t=4 3 2 1 [0,1][1,2][2,3][3,4]
t1=3 2 1 0 [0][1][2][3]
4 阿里巴巴 JSONObject 实现的clone
public Object clone() { return new JSONObject((Map)(this.map instanceof LinkedHashMap?new LinkedHashMap(this.map):new HashMap(this.map)));}
阅读全文
0 0
- 【面试】--java 基础Cloneable 最彻底的clone是什么?
- Java Clone and Cloneable
- java 重写接口Cloneable的Clone方法 拷贝对象
- Java中Cloneable 和 clone()的总结和使用
- 最基础的java面试
- java面试基础-Java中对Clone的理解
- clone() and the Cloneable Interface in Java
- Java中的clone()和Cloneable接口
- Cloneable 和 clone()的总结和使用
- Cloneable 和 clone()的总结和使用
- Cloneable接口和Object的clone()方法
- java.lang.Cloneable 实现深clone和浅clone
- Java中的Cloneable接口下的clone深拷贝,单纯的获取那个时刻的值
- Java对象克隆(Clone)及Cloneable接口、Serializable接口的深入探讨
- Java对象克隆(Clone)及Cloneable接口、Serializable接口的深入探讨
- Java对象克隆(Clone)及Cloneable接口、Serializable接口的深入探讨
- Java对象克隆(Clone)及Cloneable接口、Serializable接口的深入探讨
- Java对象克隆(Clone)及Cloneable接口、Serializable接口的深入探讨
- BM算法
- 摩尔定律还有效吗?
- 算法竞赛入门经典第二版 2-6 permutation
- 手机安卓谷歌服务
- 在VS2105中设置QT程序的图标ICON 和程序系统任务栏图标
- 【面试】--java 基础Cloneable 最彻底的clone是什么?
- opencv ldd not found opencv***lib
- [BZOJ]4503 两个串:我的第一次FFT尝试
- 萌新cache2go源码解读
- [bzoj1009][kmp][DP][矩阵乘法]GT考试
- HDU 1181 变形课 (DFS)
- ListView嵌套
- centos7 yum安装Nginx
- python入门----浅谈数据类型