enum Enum in Java

Source: Internet
Author: User

 Public classTestenum {/*the most common enumeration*/     Public enumcolorselect {red, green, yellow, blue; }    /*enumerations can also add methods and properties like normal classes, and you can add static and non-static properties or methods to it, all as you do in a generic class.*/     Public enumSeason {//enum list must be written first, or compile errorwinter, Spring, summer, fall; Private Final StaticString location = "Phoenix";  Public StaticSeason getbest () {if(Location.equals ("Phoenix"))                returnWinter; Else                returnSummer; }    }    /*You can also have a construction method*/     Public enumTemp {/*The value is assigned by parentheses, and must have a parameter constructor and a property with the method, otherwise the compilation error * Assignment must be assigned or not assigned value, not part of the assignment is not assigned a value * if not assigned to the constructor, the assignment compiler also error*/Absolutezero (-459), freezing (+), boiling (212), Paperburns (451); Private Final intvalue;  Public intGetValue () {returnvalue; }        //constructors can only be private by default, ensuring that constructors are only used internallyTemp (intvalue) {             This. Value =value; }    }     Public Static voidMain (string[] args) {/** An enumeration type is a type that defines a variable to restrict the assignment of a variable by "enumeration name. Value" To get the value in the relevant enumeration*/ColorSelect m=Colorselect.blue; Switch(m) {/*Note: Weightlifting wrote ToString (), saying that the value of the enumeration variable is not prefixed with * so it is blue instead of Colorselect.blue*/      CaseRed:System.out.println ("Color is red");  Break;  CaseGreen:System.out.println ("Color is green");  Break;  CaseYellow:System.out.println ("Color is yellow");  Break;  CaseBlue:System.out.println ("Color is blue");  Break; } System.out.println ("Traversing values in ColorSelect"); /*get an array of enumeration values by values ()*/         for(ColorSelect c:colorselect.values ()) {System.out.println (c); } System.out.println (The values in the enumeration ColorSelect are: "+colorselect.values (). length+" a "); /*ordinal () returns the index position of the enumeration value in the enumeration, starting at 0*/System.out.println (ColorSelect.red.ordinal ());//0System.out.println (ColorSelect.green.ordinal ());//1System.out.println (ColorSelect.yellow.ordinal ());//2System.out.println (ColorSelect.blue.ordinal ());//3    /*enumeration implements the Java.lang.Comparable interface by default*/System.out.println (ColorSelect.red.compareTo (Colorselect.green));                System.out.println (Season.getbest ());  for(Temp t:temp.values ()) {/*get the value of the associated enumeration by GetValue ()*/System.out.println (t+ "value is" +T.getvalue ()); }    }}

enum Enum in Java

Related Article

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.