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

推荐订阅源

有赞技术团队
有赞技术团队
MyScale Blog
MyScale Blog
Cyberwarzone
Cyberwarzone
Schneier on Security
Schneier on Security
I
Intezer
Cisco Talos Blog
Cisco Talos Blog
Cloudbric
Cloudbric
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
NISL@THU
NISL@THU
博客园 - Franky
F
Fortinet All Blogs
Google DeepMind News
Google DeepMind News
Jina AI
Jina AI
T
The Exploit Database - CXSecurity.com
P
Proofpoint News Feed
N
News and Events Feed by Topic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
F
Full Disclosure
T
Troy Hunt's Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Project Zero
Project Zero
P
Palo Alto Networks Blog
Recorded Future
Recorded Future
美团技术团队
D
Docker
PCI Perspectives
PCI Perspectives
Microsoft Azure Blog
Microsoft Azure Blog
MongoDB | Blog
MongoDB | Blog
L
LINUX DO - 最新话题
Recent Announcements
Recent Announcements
Hacker News: Ask HN
Hacker News: Ask HN
人人都是产品经理
人人都是产品经理
月光博客
月光博客
D
DataBreaches.Net
The Hacker News
The Hacker News
爱范儿
爱范儿
V
Visual Studio Blog
Engineering at Meta
Engineering at Meta
SecWiki News
SecWiki News
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
A
About on SuperTechFans
Latest news
Latest news
GbyAI
GbyAI
T
Tor Project blog
L
LINUX DO - 热门话题
Security Latest
Security Latest
博客园 - 聂微东
Y
Y Combinator Blog
AI
AI
M
MIT News - Artificial intelligence

博客园 - 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;
}