SerialCloneable

序列化机制提供了一种克隆对象的简便途径,但是很慢!!!!


package xml;

import java.io.Serializable;
import java.util.Date;
import java.util.GregorianCalendar;
import java.io.*;

public class SerialCloneTest {
public static void main(String[] args){
Employee harry=new Employee("harry",2900,1989,1,1);
Employee newH=(Employee)harry.clone();

System.out.println(newH);
}
}
class SerialCloneable implements Serializable,Cloneable{
public Object clone(){
try{
ByteArrayOutputStream bout=new ByteArrayOutputStream();
ObjectOutputStream out=new ObjectOutputStream(bout);
out.writeObject(this);
out.close();

ByteArrayInputStream bin=new ByteArrayInputStream(bout.toByteArray());
ObjectInputStream in=new ObjectInputStream(bin);
Object ret=in.readObject();
in.close();

return ret;
}catch(Exception e){
return null;
}
}
}
class Employee extends SerialCloneable{
private String name;
private double salary;
private Date hireDay;

public Employee(){}
public Employee(String n,double s,int y,int m,int d){
this.name=n;
this.salary=s;
GregorianCalendar calendar=new GregorianCalendar(y,m-1,d);
this.hireDay=calendar.getTime();
}

public String getName(){
return this.name;
}
public double getSalary(){
return this.salary;
}
public Date getHireDay(){
return this.hireDay;
}

public String toString(){
return getClass().getName()+"[name="+name+",salary="+this.salary+",hireday="+this.hireDay+"]";
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值