ObjectInputStream、ObjectOutputStream实现对象的克隆

    CloneUtil类:

package com.bijian.study.clone;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class CloneUtil {

    public static void main(String[] args) {
        //创建一个对象
        TemplateObject templateObject = new TemplateObject();
        templateObject.setAge(10);
        templateObject.setName("test");
        ArrayList<String> descriptions = new ArrayList<String>();
        descriptions.add("是一个好人");
        descriptions.add(new String("好姑娘"));
        descriptions.add(new String("硕士毕业"));
        descriptions.add(new String("白富美"));
        templateObject.setDescriptionList(descriptions);
        
        Style style = new Style();
        style.setLength(1600);
        style.setType("欧美风");
        Clothing clothing = new Clothing();
        clothing.setColor("红色");
        clothing.setSize(160);
        clothing.setStyle(style);
        templateObject.setClothing(clothing);
        
        System.out.println(templateObject);
        
        TemplateObject cloneTemplateObject = (TemplateObject) CloneUtil.getCloneObject(templateObject);
        
        System.out.println(cloneTemplateObject);
        
        System.out.println("templateObject==templateObject:" + (templateObject == cloneTemplateObject));
        System.out.println("templateObject.equals(templateObject):" + (templateObject.equals(cloneTemplateObject)));
        System.out.println("templateObject.toString().equals(templateObject.toString()):" + (templateObject.toString().equals(cloneTemplateObject.toString())));
    }
    
    // 克隆对象
    public static Object getCloneObject(Object bean) {
        Object cloneBean = null;
        try {
            ByteArrayOutputStream byout = new ByteArrayOutputStream();
            ObjectOutputStream obj = new ObjectOutputStream(byout);
            obj.writeObject(bean);
            ByteArrayInputStream byin = new ByteArrayInputStream(byout.toByteArray());
            ObjectInputStream ins = new ObjectInputStream(byin);
            cloneBean = (Object) ins.readObject();
        } catch (Exception ex) {
            System.err.print(ex);
        }
        return cloneBean;
    }
}

   TemplateObject类:

package com.bijian.study.clone;

import java.io.Serializable;
import java.util.ArrayList;

public class TemplateObject implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 6790908190923407706L;
    
    private int age;
    private String name;
    private ArrayList<String> descriptionList;
    private Clothing clothing;
    
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public ArrayList getDescriptionList() {
        return descriptionList;
    }
    public void setDescriptionList(ArrayList descriptionList) {
        this.descriptionList = descriptionList;
    }
    public Clothing getClothing() {
        return clothing;
    }
    public void setClothing(Clothing clothing) {
        this.clothing = clothing;
    }
    
    public String toString() {
        
        String baseInfo = String.valueOf(age) + name;
        
        String descriptions = "";
        for(int i=0;i<descriptionList.size();i++) {
            descriptions += descriptionList.get(i).toString();
        }
        
        return baseInfo + descriptions + clothing;
    }
}

      Clothing类: 

package com.bijian.study.clone;

import java.io.Serializable;

public class Clothing implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -2876485615924815655L;
    private String color;
    private int size;
    private Style style;
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public int getSize() {
        return size;
    }
    public void setSize(int size) {
        this.size = size;
    }
    public Style getStyle() {
        return style;
    }
    public void setStyle(Style style) {
        this.style = style;
    }
    
    public String toString() {
        
        return color + String.valueOf(size) + style;
    }
}

      Style类:

package com.bijian.study.clone;

import java.io.Serializable;

public class Style implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -7052770518598516803L;
    private String type;
    private int length;
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public int getLength() {
        return length;
    }
    public void setLength(int length) {
        this.length = length;
    }
    
    public String toString() {
        
        return type + String.valueOf(length);
    }
}

运行结果:

10test是一个好人好姑娘硕士毕业白富美红色160欧美风1600
10test是一个好人好姑娘硕士毕业白富美红色160欧美风1600
templateObject==templateObject:false
templateObject.equals(templateObject):false
templateObject.toString().equals(templateObject.toString()):true

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值