-1

我想弄清楚爲什麼我的代碼拋出了「java.io.WriteAbortedException:寫入中止; java.io.NotSerializableException:core.FlightOrder $ FlightTicket」異常。可序列化的內部類

我上課聲明:

public class FlightOrder implements Serializable 
包含FlightTickets的私人設置

而一個內部類聲明:

public class FlightTicket 

我讀到的解決方案,是使內部類「FlightTicket」靜態類,但林不知道多數民衆贊成我supose到確保我的代碼正常工作。 有人可以幫我弄清楚什麼是解決這個問題的正確方法嗎?

public class FlightTicket 
{ 
    /** 
    * The passenger for this ticket 
    */ 
    private Customer passenger; 
    /** 
    * passenger's seat 
    */ 
    private int seat; 
    /** 
    * passenger's row 
    */ 
    private int row; 
    /** 
    * The passenger's class in the plane 
    */ 
    private E_ClassType classType; 

    /** 
    * Full constructor 
    * @param passenger 
    * @param seat 
    * @param row 
    * @param classType 
    */ 
    public FlightTicket(Customer passenger, int seat, int row , String classType) 
    { 
     this.passenger = passenger; 
     setSeat(seat); 
     setRow(row); 
     setClassType(classType); 
    } 

    /** 
    * Partial constructor 
    * @param seat 
    * @param row 
    */ 
    public FlightTicket(int seat, int row) 
    { 
     setSeat(seat); 
     setRow(row); 
    } 

    //-------------------------------Getters And Setters------------------------------ 
    /** 
    * seat has to be positive number 
    * @param seat 
    */ 
    public void setSeat(int seat) { 
     if(seat>0) 
      this.seat = seat; 
    } 


    /** 
    * row has to be positive number 
    * @param row 
    */ 
    public void setRow(int row) { 
     if(row>0) 
      this.row = row; 
    } 

    /** 
    * 
    * @return classType 
    */ 
    public E_ClassType getClassType() { 
     return classType; 
    } 

    public int getSeat(){ 
     return seat; 
    } 
    public int getRow(){ 
     return row; 
    } 

    /** 
    * set the class type from the array classType located in Constants. 
    * if the classType not exists, the default value is Economy. 
    * @param classType 
    */ 
    public void setClassType(String classType) { 

     for(E_ClassType c : E_ClassType.values()) 
     { 
      if(c.toString().equals(classType)) 
      { 
       this.classType = E_ClassType.valueOf(classType); 
       return; 
      } 
     } 
     this.classType = E_ClassType.Tourists; 

    } 

    public FlightOrder getOuterType() { 
     return FlightOrder.this; 
    } 


    /* (non-Javadoc) 
    * @see java.lang.Object#hashCode() 
    */ 
    @Override 
    public int hashCode() { 
     final int prime = 31; 
     int result = 1; 
     result = prime * result + getOuterType().hashCode(); 
     result = prime * result + row; 
     result = prime * result + seat; 
     return result; 
    } 

    /* (non-Javadoc) 
    * @see java.lang.Object#equals(java.lang.Object) 
    */ 
    @Override 
    public boolean equals(Object obj) { 
     if (this == obj) 
      return true; 
     if (obj == null) 
      return false; 
     if (getClass() != obj.getClass()) 
      return false; 
     FlightTicket other = (FlightTicket) obj; 
     if (!getOuterType().equals(other.getOuterType())) 
      return false; 
     if (row != other.row) 
      return false; 
     if (seat != other.seat) 
      return false; 
     return true; 
    } 

    /* (non-Javadoc) 
    * @see java.lang.Object#toString() 
    */ 
    @Override 
    public String toString() { 
     return "FlightTicket [passenger=" + passenger + ", seat=" + seat 
       + ", row=" + row + ", flight number=" + getFlight().getFlightNumber() + "]"; 
    } 

}// ~ END OF Inner Class FlightTicket 
+1

使'FlightTicket'也實現'Serializable'? – 2014-08-30 20:05:43

+0

@AlanStokes試過了。沒有工作,根據我已閱讀的內容,你不打算這樣做。 – Roi811 2014-08-30 20:07:32

+0

內部/靜態無關緊要,如果你想序列化一個對象,那麼它必須是可串行化的。 – 2014-08-30 20:11:45

回答

0

你只需要通過所有將被序列化的類來實現Serializable接口。我的意思是在你的可序列化類中聲明的所有類的實例變量也應該實現Serializable。

在你的情況FlightTicket和客戶需要實現Serializable。

+0

他們都是。這不是這種情況下的問題。謝謝你的幫助! – Roi811 2014-08-30 20:21:42

2

使內部類Serializable將工作,這就是你正在假設要做,如果你想要序列化它與外部類一起。以下代碼演示:

public class Outer implements Serializable { 
    class Inner implements Serializable { 
     int value = 17; 
    } 

    Inner inner = new Inner(); 

    public static void main(String[] args) throws Exception { 
     ByteArrayOutputStream out = new ByteArrayOutputStream(); 
     ObjectOutputStream oout = new ObjectOutputStream(out); 
     Outer obj = new Outer(); 
     obj.inner.value = 22; 
     oout.writeObject(obj); 
     Outer r = (Outer) new ObjectInputStream(new ByteArrayInputStream(
       out.toByteArray())).readObject(); 
     System.out.println(r.inner.value); 
    } 
} 

輸出爲22,該值已正確序列化並從內部類字段反序列化。

+0

當兩者都序列化時,我在FlightTicket hashcode方法中通過'getOuterType()'得到'NullPointerException'。可能知道爲什麼? – Roi811 2014-08-30 20:20:36