Java enumeration types

Source: Internet
Author: User

  1. public class Testenum {
    /* The most common enumeration */
    public enum ColorSelect {
    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 enum Season {
    Enum list must be written first, or compile 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 construction method */
    public enum Temp {
    /* Assign values by parentheses, and must have a parameter constructor and a property followed method, otherwise compilation error
    * Assignments must be either assigned or not assigned, and cannot be assigned a part of a value
    * If you do not assign a value, you cannot write the constructor, and the assignment is wrong.
    Absolutezero ( -459), freezing (+), boiling (212), Paperburns (451);

    private final int value;
    public int GetValue () {
    return value;
    }
    Constructors can only be private by default, ensuring that constructors are only used internally
    Temp (int value) {
    This.value = value;
    }
    }


    public static void Main (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
    * So for blue, not 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 ("Traverse values in ColorSelect");
    /* Get an array of enumeration values through 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 ());//0
    System.out.println (ColorSelect.green.ordinal ());//1
    System.out.println (ColorSelect.yellow.ordinal ());//2
    System.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 relevant enumeration by GetValue () */
    The value of System.out.println (t+ "is" +t.getvalue ());
    }


    }
    }

Java enumeration types

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.