Complex initialization in Java
Although boring, it is still listed.
6.1 Memento1 code in memorandum mode (5.6)
Routines 7-2 unchanged class package intent. memento; public final class Memento1 {private final VirtualState state State; // the state to be remembered, public Memento1 (state State state) {this. state = new VirtualState (state);} State getState () {State state State = new state (this. state. x, this. state. y); return state;}/*** deep cloning technology can be used. */Class VirtualState {private int x; private int y; public VirtualState (State state) {x = state. getX (); y = State. getY ();}}}In the program
VirtualStateIt is intended to remind programmers that the saved state is not the same object as the Originator Member state.
In fact, yqj2065 will never design a redundant class drop, or use State.
Therefore, the corresponding code is available:
Package intent. memento; public final class Memento2 {private final State state State; // the state to be remembered, public Memento2 (State state State) {this. state = new State () {setX (state. getX (); setY (state. getY () ;}};} State getState () {return new State () {setX (state. getX (); setY (state. getY ());}};}}A boring thing here
new State(){{setX(state.getX());setY(state.getY());}};In Introduction to programming (Java) p80, we also mentioned
{{;}};
Class external initialization block.