惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Y
Y Combinator Blog
The GitHub Blog
The GitHub Blog
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
Google DeepMind News
Google DeepMind News
aimingoo的专栏
aimingoo的专栏
Recent Announcements
Recent Announcements
A
About on SuperTechFans
U
Unit 42
MyScale Blog
MyScale Blog
J
Java Code Geeks
博客园_首页
Blog — PlanetScale
Blog — PlanetScale
D
Docker
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 司徒正美
量子位
月光博客
月光博客
G
Google Developers Blog
V
V2EX
博客园 - 聂微东
宝玉的分享
宝玉的分享
IT之家
IT之家
Vercel News
Vercel News

博客园 - yongwnet

项目开发文档格式 JAVA反射实例 java.util.Properties类 jQuery调用WebService详解 产品经理的素质 JAVA 调用 .NET编写的WebService - yongwnet Java中实现鼠标模拟与键盘映射 Win7下轻松注册VS2008 Windows7 下的VPC 浅谈项目管理能力的提升 java多态性详解——父类引用子类对象 Java截取字符串的一些常用处理 java类加载的表现形式 extjs COMBOBOX完成类似GOOGLE搜索框(AUTOCOMPLETE) - yongwnet - 博客园 如何有效的压缩VPC虚拟磁盘 VPC 2007 Console界面消失以及解决方法 - yongwnet JAVA匿名实现多线程 - yongwnet - 博客园 java中list类和vector类的比较
JAVA深克隆
yongwnet · 2010-04-03 · via 博客园 - yongwnet

代码


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;/**
 * 
@author wangyong
 *
 
*/
public class FormTest {public static Object objectCopy(Object sourceObj) //throws CloneNotSupportedException
    {
                
        
//实现深克隆(即深复制)
        try
        {
            
//将对象写到流里
            ByteArrayOutputStream bo=new ByteArrayOutputStream();
             ObjectOutputStream oo
=new ObjectOutputStream(bo);
             oo.writeObject(sourceObj);
//源对象
             
//从流里读出来
             ByteArrayInputStream bi=new ByteArrayInputStream(bo.toByteArray());
             ObjectInputStream oi
=new ObjectInputStream(bi);
             
return(oi.readObject());//目标对象
        }
        
catch(Exception ex)
        {
            ex.printStackTrace();
            
return null;
        }

    }

/**
     * 
@param args
     
*/
    
public static void main(String[] args) {
        
// TODO 自动生成方法存根
        
        
        
try
        {
            B b1 
= new B();
            b1.sb
="b1";
            B b2 
= new B();
            b2.sb
="b2";
            A a1 
= new A(b1);
            A a2 
= new A(b2);
            
            a1.s
="wy";
            a1.o
="obj a1";
            a2
=(A)a1.clone();//或 objectCopy(a1);
            
//a2.b.sb="456";
            a2.s="a2";
            a2.b.sb
="new b2";
            a2.o
="obj a2";
            String ss 
= a2.s;
            
            
//实现深复制之后, a1 中的对象 b 不会受到 a2 的重新赋值而改变;

            
        }
        
catch(Exception ex)
        {
            ex.printStackTrace();
            ex.getMessage();
        }
        
//说明:要序列化的类需要实现 Serializable 接口;
         
//要克隆的类需要实现 Cloneable 接口;
        
    }

}

class A implements Cloneable,Serializable
{
    
public    String s = null;
    
public Object o = null;
    
public B b=null;
        
    A(B bb)
    {
        b 
= new B();
        b.sb
=bb.sb;
    }
public Object clone() //throws CloneNotSupportedException
    {        
        
try
        {
            
/*//浅克隆(即浅复制)
            A a = (A) super.clone();            
            return a;
*/
            
            
//实现深克隆(即深复制)
            
//将对象写到流里
            ByteArrayOutputStream bo=new ByteArrayOutputStream();
             ObjectOutputStream oo
=new ObjectOutputStream(bo);
             oo.writeObject(
this);//序列化当前类
             
//从流里读出来
             ByteArrayInputStream bi=new ByteArrayInputStream(bo.toByteArray());
             ObjectInputStream oi
=new ObjectInputStream(bi);
             
return(oi.readObject());
        }
        
catch(Exception ex)
        {
            ex.printStackTrace();
            
return null;
        }

    }

}

class B implements Serializable
{
    String sb 
= null;
}