Java Learning 05

Source: Internet
Author: User

  

 Packageday20140918;Importorg.junit.Test;/** As a result of yesterday's interview asked a singleton mode * Review * single-case design mode **///a hungry man, class one load exists, thread-safeclasssinglee{//define a private member for easy testing    Private intnum = 1; Private Static FinalSinglee single =NewSinglee (); PrivateSinglee () {num++; }         Public StaticSinglee getinstance () {returnSingle ; }         Public intgetInt () {returnnum; }}//lazy, post-load, thread insecureclasssinglel{PrivateSinglel () {}Private StaticSinglel single =NULL;  Public StaticSinglel getinstance () {//two layer judgment, improve the efficiency of the program, do not add this layer of judgment, every time you have to judge the lock, program efficiency is very low        if(single =NULL){            //Lock, prevent multi-threading, generate multiple single at the same time, no guarantee of uniqueness            synchronized(Singlel.class) {                if(single =NULL) { single=NewSinglel (); }            }        }        returnSingle ; }} Public classSingledemo {@Test Public voidTest () {Singlee S1=singlee.getinstance ();        System.out.println (S1.getint ()); //2Singlee s2 =singlee.getinstance ();        System.out.println (S1.getint ()); //2System.out.println (S2.getint ());//2            }    }

In addition, the enumeration type enum that was added to version 1.5 in Java was reviewed. Enumerations are special classes that can be used when we use some data that requires only a certain number of values. When there is only one object in the enumeration, it is actually a singleton mode!! The others do not say, on the code ~

 Packageday20140918;Importorg.junit.Test;/** Enumerate enum's learning, write an enumeration that represents the week, inside there is a method that returns the Chinese name of the object week. *  * */ Public classSyudy {//defines an enumeration that represents Monday through Sunday    enumweekday{//7 ObjectsMON ("Monday"), TUE ("Tuesday"), WED ("Wednesday"), THU ("Thursday"), FRI ("Friday"), SAT ("Saturday"), SUN ("Sunday"); PrivateString Weekname; //The constructor must be private because the enumeration can only take a defined value, and the external cannot be a new object        PrivateWeekDay (String weekname) { This. Weekname =Weekname; }                 PublicString GetName () {returnWeekname; }} @Test Public voidtest1 () {System.out.println (weekday.tue); //TUESystem.out.println (WeekDay.TUE.getName ());//Tuesday    }            //the second method, using abstract functions    enumweekday1{MON () { PublicString Getweekname () {return"Monday"; }}, tue{ PublicString Getweekname () {return"Tuesday"; }}, wed{ PublicString Getweekname () {return"Wednesday"; }}, thu{ PublicString Getweekname () {return"Thursday"; }}, fri{ PublicString Getweekname () {return"Friday"; }}, sat{ PublicString Getweekname () {return"Saturday"; }}, sun{ PublicString Getweekname () {return"Sunday";                        }        }; //define an abstract method to get a Chinese name for the week         Public AbstractString getweekname (); } @Test Public voidtest2 () {WeekDay1 Fri=Weekday1.fri;       System.out.println (Fri.getweekname ()); //Friday//The offset of the enumeration, starting from 0System.out.println (Fri.ordinal ());//4//all objects that get enumeratedWeekDay1 [] WDS =weekday1.values ();  for(WeekDay1 wd:wds) {System.out.println (Wd.getweekname ()); }                /*Monday Tuesday Wednesday Thursday Friday Saturday Sunday*/    }}

Learning is a continuous accumulation, and constantly adhere to the process, I hope that every day can feel their progress, work will become more substantial ~ learning time to the heart, know it, but also to know its why, this is my own requirements, try to grasp every detail. Yesterday's interview also proves that the details determine success or failure ~

Java Learning 05

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.