编写我们的实验代码,该代码用于实现我们的一个克隆实例,具体如下图所示,代码如下:
public static void main(String[] args) {
System.out.println("序列化之前");
Address address=new Address("中国","吉林","长春");
Employees employee1=new Employees("小明", 30, address);
System.out.println("员工1的信息:");
System.out.println(employee1);
System.out.println("序列化之后:");
ObjectOutputStream out=null;
ObjectInputStream in=null;
Employees employee2=null;
try{
out=new ObjectOutputStream(new FileOutputStream("employees.dat"));
out.writeObject(employee1);
in=new ObjectInputStream(new FileInputStream("employees.dat"));
employee2=(Employees)in.readObject();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}catch(ClassNotFoundException e){
e.printStackTrace();
}finally{
}
employee2.getAddress().setState("中国");
employee2.getAddress().setPrivance("四川");
employee2.getAddress().setCity("成都");
employee2.setName("大明");
employee2.setAge(24);
System.out.println("员工1的信息:");
System.out.println(employee1);
System.out.println("员工2的信息:");
System.out.println(employee2);
}