Enumeration type in Java

Source: Internet
Author: User
Public class testenum {/* the most common enumeration */Public Enum colorselect {red, green, yellow, blue;}/* The enumeration can also add methods and attributes like a general class, you can add static and non-static attributes or methods for it, as you do in general classes. */Public Enum season {// The enumeration list must be written at the beginning; otherwise, the compilation error winter, spring, summer, fall; private final static string location = "Phoenix "; public static season getbest () {If (location. equals ("Phoenix") return winter; else return summer ;}}/* can also have a constructor */Public Enum temp {/* assign values through parentheses, besides, there must be a constructor with parameters and an attribute and method. Otherwise, an error occurs in compilation. * values must be both assigned values or not assigned values, some values cannot be assigned. * If values are not assigned, the constructor cannot be written. An error occurs during value assignment and compilation. */absolutezero (-459), freezing (32), boiling (212 ), paperburns (451); private final int value; Public int getvalue () {return value;} // The constructor can only be private by default, so that the constructor can only use temp (INT value) {This. value = value ;}} public static void main (string [] ARGs) {/** the enumerated type is a type used to define variables, when you assign a value to a variable, use the "enumeration name. value "to obtain the value in the related enumeration */colorselect M = colorselect. blue; Switch (m) {/* Note: The enumeration overwrites tostring (), which means that the value of the enumerated variable is not prefixed * so it is blue rather than colorselect. blue */case red: system. out. println ("color is red"); break; Case Green: system. out. println ("color is green"); break; Case Yellow: system. out. println ("color is yellow"); break; Case Blue: system. out. println ("color is blue"); break;} system. out. println ("traversing values in colorselect");/* obtain an array of enumerated values through values () */For (colorselect C: colorselect. values () {system. out. println (c);} system. out. println ("the enumerated values in colorselect are:" + colorselect. values (). length + "");/* ordinal () returns the index position of the enumerated value in the enumeration, starting from 0 */system. out. println (colorselect. red. ordinal (); // 0 system. out. println (colorselect. green. ordinal (); // 1 system. out. println (colorselect. yellow. ordinal (); // 2 system. out. println (colorselect. blue. ordinal (); // 3/* enumeration implements Java by default. lang. comparable interface */system. out. println (colorselect. red. compareto (colorselect. green); system. out. println (season. getbest (); For (temp T: temp. values () {/* Get the enumerated value through getvalue () */system. out. the value of println (t + "is" + T. getvalue ());}}}

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.