欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ObjectInputStream、ObjectOutputStream实现对象的克隆

程序员文章站 2022-04-03 19:53:52
...

    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