我有一個類叫做Flight Flight類在實例化時,實例化另一個名爲SeatingChart的類,而SeatingChart也實例化另一個類等等等等。如何反序列化對象?
public class Flight implements Serializable
{
SeatingChart sc = new SeatingChart(); seating
//WaitingList wl = new WaitingList();
}
public class SeatingChart extends ListAndChart implements PassengerList, Serializable
{
public static final int NOT_FOUND = 42;
Passenger [] pass = new Passenger[40];
}
public class Passenger implements Serializable
{
private String firstName, lastName, fullName;
public String getName()
{
fullName = firstName + " " + lastName;
return fullName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
}
我在另一個類的另一種方法是反序列化保存在磁盤中的對象
public void actionPerformed(ActionEvent evt)
{
Serialization.deserialize(sw101); <--- sw101 is a Flight object
.
.
.
}
//code for deserialization
public static void deserialize(Flight sw101)
{
String filename = "serialized.ser";
sw101 = null;
FileInputStream fis = null;
ObjectInputStream in = null;
try
{
fis = new FileInputStream(filename);
in = new ObjectInputStream(fis);
sw101 = (Flight)in.readObject();
System.out.println("sw101" + sw101.toString());
in.close();
}
catch(IOException ex)
{
ex.printStackTrace();
}
catch(ClassNotFoundException ex)
{
ex.printStackTrace();
}
}
我的問題是,當我分配SW101序列化的對象,一切SW101在開始實例像SeatingChart SC如果這些對象都實現了Serializable接口,那麼對象也可以獲取文件中保存的任何內容,而無需執行任何操作。如果是這樣,爲什麼我的代碼不工作?我究竟做錯了什麼?
定義... 「不工作」。它做什麼呢? – trutheality 2011-06-16 23:49:07
另外,最好有一個「私有靜態最終的long serialVersionUID」字段。 – trutheality 2011-06-16 23:51:51
它不會返回序列化對象。當我嘗試打印乘客的名字時,它不起作用。 – dave 2011-06-16 23:51:55