// class类需要先实现 implements Serializable 接口;
//类当中的其他复杂类属性也需要 实现 此接口 implements Serializable
//后定义下列方法
public ListClassRoomUsed clone(){
ListClassRoomUsed copy = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(this);
ByteArrayInputStream bais=new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois=new ObjectInputStream(bais);
copy=(ListClassRoomUsed) ois.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return copy;
}
Java通过序列化克隆对象
最新推荐文章于 2024-05-23 18:18:25 发布