可序列化的内部类

时间:2014-08-30 20:01:58

标签: java static inner-classes serializable notserializableexception

我试图找出为什么我的代码抛出了一个" java.io.WriteAbortedException:写入已中止; java.io.NotSerializableException:core.FlightOrder $ FlightTicket"异常。

我将类声明为:

public class FlightOrder implements Serializable 

包含一组私人FlightTickets。

内部类声明为:

public class FlightTicket

我读到的解决方案是制作内部班级" FlightTicket"一个静态类,但我不确定这是我为了使我的代码正常工作所做的事情。 有人可以帮我弄清楚什么是解决这个问题的正确方法吗?

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

2 个答案:

答案 0 :(得分: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,已从内部类字段正确序列化和反序列化的值。

答案 1 :(得分:0)

您只需要将所有将被序列化的类实现Serializable接口。我的意思是在可序列化类中声明的所有实例变量类也应该实现Serializable。

在您的情况下,FlightTicket和Customer需要实现Serializable。