📄 abstractfoo.java
字号:
// 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
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -