Complex initialization in Java

Source: Internet
Author: User

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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.