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 descriptions = new ArrayList();
        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 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 
        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;
    }
}
  
스타일 클래스: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
  
  이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
프로그램이 설치된 HDD의 데이터를 다른 HDD로 전송하는 방법시스템 드라이브가 아닌 물리적 모바일 설치 프로그램의 드라이브 데이터 여기에 갑자기 동적 디스크로 변한 HDD가 하나 있다. 동적 디스크는 여러 가지 장점이 있는 것 같지만 다른 환경에서 내용을 볼 수 없다는 단점도...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.