我想弄清楚爲什麼我的代碼拋出了「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
使'FlightTicket'也實現'Serializable'? – 2014-08-30 20:05:43
@AlanStokes試過了。沒有工作,根據我已閱讀的內容,你不打算這樣做。 – Roi811 2014-08-30 20:07:32
內部/靜態無關緊要,如果你想序列化一個對象,那麼它必須是可串行化的。 – 2014-08-30 20:11:45