中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Java > java高级编程
深层克隆
作者:未知 时间:2005-07-24 21:15 出处:JR 责编:chinaitpower
              摘要:深层克隆
1、表层克隆

  1. public class  Snake implements Cloneable
  2. {
  3.     private Snake next;
  4.     private char c;
  5.     Snake(int i, char x){
  6.         c = x;
  7.         if(--i > 0){
  8.              next = new Snake(i,(char)(x+1));
  9.         }
  10.     }
  11.     public void increment(){
  12.         c++;
  13.         if(next!=null){
  14.             next.increment();
  15.         }
  16.     }
  17.     public String toString(){
  18.         String s = ":"+c;
  19.         if(next!=null){
  20.             s += next.toString();
  21.         }
  22.         return s;
  23.     }
  24.     public Object clone(){
  25.         Object o = null;
  26.         try{
  27.             o = super.clone();
  28.         }catch(CloneNotSupportedException e){}        
  29.         return o;
  30.     }
  31.     public static void main(String[] args) 
  32.     {
  33.         Snake s = new Snake(5,'a');
  34.         System.out.println("s ="+s);
  35.         Snake s2 = (Snake)s.clone();
  36.         System.out.println("s2 ="+s2);
  37.         s2.increment();
  38.         System.out.println("after s2.increment, s="+s+"  s2 ="+s2);
  39.     }
  40. }

此时的输出结果:
s =:a:b:c:d:e
s2 =:a:b:c:d:e
after s2.increment, s=:a:c:d:e:f  s2 =:b:c:d:e:f
2、深层克隆

  1. public class  Snake implements Cloneable
  2. {
  3.     private Snake next;
  4.     private char c;
  5.     Snake(int i, char x){
  6.         c = x;
  7.         if(--i > 0){
  8.              next = new Snake(i,(char)(x+1));
  9.         }
  10.     }
  11.     public void increment(){
  12.         c++;
  13.         if(next!=null){
  14.             next.increment();
  15.         }
  16.     }
  17.     public String toString(){
  18.         String s = ":"+c;
  19.         if(next!=null){
  20.             s += next.toString();
  21.         }
  22.         return s;
  23.     }
  24.     public Object clone(){
  25.         Snake o = null;
  26.         try{
  27.             o = (Snake)super.clone();
  28.         }catch(CloneNotSupportedException e){}
  29.         if(o.next !=null)
  30.         o.next = (Snake)o.next.clone();
  31.         return o;
  32.     }
  33.     public static void main(String[] args) 
  34.     {
  35.         Snake s = new Snake(5,'a');
  36.         System.out.println("s ="+s);
  37.         Snake s2 = (Snake)s.clone();
  38.         System.out.println("s2 ="+s2);
  39.         s2.increment();
  40.         System.out.println("after s2.increment, s="+s);
  41.         System.out.println("s2 ="+s2);
  42.     }
  43. }

此时的输出结果为:
s =:a:b:c:d:e
s2 =:a:b:c:d:e
after s2.increment, s=:a:b:c:d:e  s2 =:b:c:d:e:f

总结:

[pre]    在colne()函数中,如果只是简单的调用一下父类的super.clone()则只是将当前类的基
本类型按位复制,克隆后的类所含有的对象句柄仍然和当前类相同。所以,如果需要进行深
层克隆,则需要在调用super.clone()之后,克隆该类含有的对象类。[/pre]
关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有