// Nonserializable stateful class allowing serializable subclass public abstract class AbstractFoo { private int x, y; // The state private boolean initialized = false; public AbstractFoo(int x, int y) { initialize(x, y); } /** * This constructor and the following method allow subclass's * readObject method to initialize our internal state. */ protected AbstractFoo() { } protected final void initialize(int x, int y) { if (initialized) throw new IllegalStateException( "Already initialized"); this.x = x; this.y = y; // ... // Do anything else the original constructor did initialized = true; } /** * These methods provide access to internal state so it can * be manually serialized by subclass's writeObject method. */ protected final int getX() { return x; } protected final int getY() { return y; } // Must be called by all public instance methods private void checkInit() throws IllegalStateException { if (!initialized) throw new IllegalStateException("Uninitialized"); } // ... // Remainder omitted }


微信网友_7565568760549376
6月22日
暂无贡献等级
湮灭圣者 LV1
2024年1月20日
15575564465 LV1
2023年9月14日
朱俪的邮件及存储 LV8
2023年3月22日
绝对零度 LV4
2022年8月2日
你脚上银铃响了 LV1
2021年10月27日
大隐隐于市 LV2
2021年7月2日
916403064 LV2
2021年6月13日
smile soft
2021年4月28日
暂无贡献等级
有理想的中年人 LV1
2021年3月16日