我有以下课程设计。我的引擎属性每次都是null,即使我已经从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;
}
答案 0 :(得分:4)
您没有指定字段e
- 它被您使用Engine e = new Engine(oxos.readInt());
创建的本地变量遮蔽。