Complex initialization in Java

Source: Internet
Author: User

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

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.