java深克隆

public class DeepClone implements Serializable {   
    
    private static final long serialVersionUID = 7265080590460397160L;
    private String name;   
    private Long age;   
  
    public Long getAge() {   
        return age;   
    }   
  
    public void setAge(Long age) {   
        this.age = age;   
    }   
  
    public String getName() {   
        return name;   
    }   
  
    public void setName(String name) {   
        this.name = name;   
    }   
    
    @Override
    public String toString() {
        return "[" + this.getName() + "," + this.getAge() + "]";
    }
  
    public Object deepClone() throws ClassNotFoundException, IOException{   
        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());   
    }   
    
    public static void main(String[] args) throws ClassNotFoundException, IOException {
        DeepClone deep = new DeepClone();
        deep.setAge(22L);
        deep.setName("lichaozhang");
        
        DeepClone deepClone = (DeepClone) deep.deepClone();
        System.out.println(deepClone);
    }
} 

 

你可能感兴趣的:(java,OO)