Although bored, it is listed.
6.1 Memo Mode (5.6), MEMENTO1 code
Routine 7-2 immutable class package Intent.memento;public final class memento1{ private final virtualstate state;//to remember state, public Memento1 (state state) { this.state =new virtualstate (state); } State getState () {state state = new State (THIS.STATE.X,THIS.STATE.Y); return state; } /** * Can use deep cloning technology. */ class virtualstate{ private int x; private int y; Public virtualstate (state state) { x = State.getx (); y = State.gety ();}} }
In the programvirtualstateIntended to remind programmers that the saved state is not the same object as the originator member.
In fact, yqj2065 will never design an extra class drop, or use state as well.
So, with the corresponding code:
Package Intent.memento;public Final class memento2{ private final state state;//to be remembered, public Memento2 State) { this.state =new State () {{SetX (State.getx ()); Sety (State.gety ());}}; } State getState () { return new state () {{SetX (State.getx ()); Sety (State.gety ());}}; }
Here's a boring thing
New State () {{SetX (State.getx ()); Sety (State.gety ());}};
In "Introduction to Programming (Java)" P80, also mentioned, the new expression after the{{ ;}};
The initialization block outside the class body.
Complex initialization in Java