0

我有以下课程设计。我的引擎属性每次都为空,即使我已经从readObject

public class Car implements Serializable {

    private int regId;
    transient Engine e;


private void writeObject(ObjectOutputStream oos) {
        try {
            oos.defaultWriteObject();
            oos.writeInt(e.horsePower);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void readObject(ObjectInputStream oxos) {
        try {    
            oxos.defaultReadObject();
            Engine e = new Engine(oxos.readInt());              

        } catch (Exception e) {
            e.printStackTrace();
            // TODO: handle exception
        }
}

public class Engine  {    
        int horsePower;    
    }
4

1 回答 1

4

您不分配该字段e- 它被您使用创建的局部变量所遮蔽Engine e = new Engine(oxos.readInt());

于 2013-10-02T20:34:05.517 回答