java中的克隆方法的使用(三类 浅、深、完美克隆 ) 博客分类: java java克隆
程序员文章站
2024-03-26 12:12:17
...
package clone; //本例子阐释了clone的魅力,影子克隆 class UnCloneA { private int i; public UnCloneA(int ii) { i = ii; } public void doublevalue() { i *= 2; } public String toString() { return Integer.toString(i); //把整数转换为字符串 } } class CloneB implements Cloneable{ //实现了克隆接口 Cloneable public int aInt; public UnCloneAA unCA = new UnCloneAA(111); public Object clone(){ //重写clone方法 CloneB o = null; try{ o = (CloneB)super.clone(); }catch(CloneNotSupportedException e){ e.printStackTrace(); } return o; } } public class CloneMain影子克隆 { public static void main(String[] a){ CloneB b1 = new CloneB(); b1.aInt = 11; System.out.println("before clone,b1.aInt = "+ b1.aInt); System.out.println("before clone,b1.unCA = "+ b1.unCA); CloneB b2 = (CloneB)b1.clone(); //b1的克隆 b2.aInt = 22; b2.unCA.doublevalue(); System.out.println("================================="); System.out.println("after clone,b1.aInt = "+ b1.aInt); System.out.println("after clone,b1.unCA = "+ b1.unCA); //已经被改变了 System.out.println("================================="); System.out.println("after clone,b2.aInt = "+ b2.aInt); System.out.println("after clone,b2.unCA = "+ b2.unCA); } } /** RUN RESULT: * before clone,b1.aInt = 11 before clone,b1.unCA = 111 * ================================= * after clone,b1.aInt = 11 after clone,b1.unCA = 222 * ================================= * after clone,b2.aInt = 22 after clone,b2.unCA = 222 */ package clone; class UnCloneAA implements Cloneable{ private int i; public UnCloneAA(int ii) { i = ii; } public void doublevalue() { i *= 2; } public String toString() { return Integer.toString(i); } public Object clone(){ //深度克隆这个类也要重写这个方法 UnCloneAA o = null; try{ o = (UnCloneAA)super.clone(); }catch(CloneNotSupportedException e){ e.printStackTrace(); } return o; } } class CloneBB implements Cloneable{ public int aInt; public UnCloneAA unCAA = new UnCloneAA(111); public Object clone(){ CloneBB o = null; try{ o = (CloneBB)super.clone(); }catch(CloneNotSupportedException e){ e.printStackTrace(); } o.unCAA = (UnCloneAA)unCAA.clone(); //深度克隆必加的一句 return o; } } public class CloneMain深度克隆 { public static void main(String[] a){ CloneBB b1 = new CloneBB(); b1.aInt = 11; System.out.println("before clone,b1.aInt = "+ b1.aInt); System.out.println("before clone,b1.unCA = "+ b1.unCAA); CloneBB b2 = (CloneBB)b1.clone(); //克隆 b2.aInt = 22; b2.unCAA.doublevalue(); System.out.println("================================="); System.out.println("after clone,b1.aInt = "+ b1.aInt); System.out.println("after clone,b1.unCA = "+ b1.unCAA); System.out.println("================================="); System.out.println("after clone,b2.aInt = "+ b2.aInt); System.out.println("after clone,b2.unCA = "+ b2.unCAA); } } /*before clone,b1.aInt = 11 before clone,b1.unCA = 111 ================================= after clone,b1.aInt = 11 after clone,b1.unCA = 111 ================================= after clone,b2.aInt = 22 after clone,b2.unCA = 222 */ package clone; class UnCloneAAA implements Cloneable{ private int i; public UnCloneAAA(int ii) { i = ii; } public void doublevalue() { i *= 2; } public String toString() { return Integer.toString(i); } public Object clone(){ UnCloneAAA o = null; try{ o = (UnCloneAAA)super.clone(); }catch(CloneNotSupportedException e){ e.printStackTrace(); } return o; } } class CloneBBB implements Cloneable{ public int aInt; public UnCloneAAA unCAAA = new UnCloneAAA(111); public Object clone(){ CloneBBB o = null; try{ o = (CloneBBB)super.clone(); }catch(CloneNotSupportedException e){ e.printStackTrace(); } o.unCAAA = (UnCloneAAA)unCAAA.clone(); return o; } } public class CloneMain完美克隆 { public static void main(String[] a){ CloneBBB b1 = new CloneBBB(); b1.aInt = 11; System.out.println("before clone,b1.aInt = "+ b1.aInt); System.out.println("before clone,b1.unCA = "+ b1.unCAAA); CloneBBB b2 = (CloneBBB)b1.clone(); b2.aInt = 22; b2.unCAAA.doublevalue(); System.out.println("================================="); System.out.println("after clone,b1.aInt = "+ b1.aInt); System.out.println("after clone,b1.unCA = "+ b1.unCAAA); System.out.println("================================="); System.out.println("after clone,b2.aInt = "+ b2.aInt); System.out.println("after clone,b2.unCA = "+ b2.unCAAA); } } /*before clone,b1.aInt = 11 before clone,b1.unCA = 111 ================================= after clone,b1.aInt = 11 after clone,b1.unCA = 111 ================================= after clone,b2.aInt = 22 after clone,b2.unCA = 222 */
上一篇: js 克隆 博客分类: javascript 克隆clone复制对象
下一篇: Java对象的浅度clone和深度clone 博客分类: javajust do itmore and more javaclonedeep克隆拷贝
推荐阅读
-
Java对象的浅度clone和深度clone 博客分类: javajust do itmore and more javaclonedeep克隆拷贝
-
java中的克隆方法的使用(三类 浅、深、完美克隆 ) 博客分类: java java克隆
-
我不知道的事——深克隆和浅克隆 博客分类: java 浅克隆深克隆克隆java致命魔术
-
Java中对象的深复制(深克隆)和浅复制(浅克隆)介绍
-
Java中对象的深复制(深克隆)和浅复制(浅克隆)介绍
-
深入浅出Java中的clone克隆方法,写得太棒了!
-
java中的浅拷贝和深拷贝 java浅拷贝深拷贝clone克隆
-
浅谈Java中的深克隆和浅克隆(阿里面试)
-
Java中对象的深复制(深克隆)和浅复制(浅克隆)之序列化
-
深入浅出Java中的clone克隆方法,写得太棒了!