java--enum enum

Source: Internet
Author: User

Enumeration
I think that a simple enumeration can also be a data type, except that the data type contains only our own custom data, which is a set of data with common attributes. As a simple example, a color can also be defined as an enumeration type, which can contain any color you customize, and when needed, it only needs to be called by an enumeration, such as the seasons (summer and winter), the week (Monday to Sunday), the number of clothes (S, M, L, XL) and so on. Data with common attributes can define enumerations.

Here's an example:
 Public classEnumtest { Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); System.out.println ("Please enter:"); String input=In.next (). toUpperCase (); Size Size=enum.valueof (Size.class, input); System.out.println ("Size=" +size); System.out.println ("Abbreviation=" +size.getabbreviation ()); if(size==Size.extra_lagre) {System.out.println ("Good job!"); }    }            enumsize{SMALL ("S"), MEDIUM ("M"), Lagre ("L"), Extra_lagre ("XL"); PrivateString abbreviation; PrivateSize (String abbreviation) { This. abbreviation=abbreviation; }         PublicString getabbreviation () {returnabbreviation; }            }    }

Common methods of enumeration

The static enum ValueOf (Class enumclass, String name) returns the specified name, to the defined enumeration constant.

String name () returns the names of enumerated constants

String toString () returns the constant name of the enumeration more commonly than name.

int ordinal () returns the position (index) of the enumeration value in the enumeration, starting at 0.

int CompareTo (E Other) returns a negative value if the enumeration constant is returned before other, or 0 if this==other, otherwise returns normal. The order in which the enumeration constants appear is given in the enum declaration.

Here's an example:

public static void main (string[] args) {            size size =size.medium;            SYSTEM.OUT.PRINTLN (size);//medium            System.out.println (Size.name ());//medium            System.out.println ( Size.ordinal ());//1            System.out.println (size.valueof ("MEDIUM"). toString ());//medium            System.out.println ( Size.values (). length);//4  Gets the enumeration length        } 

Traversal of enumerations

public static void main (string[] args) {        for(size size:Size.values ()) {            System.out.println (size);        }    }

Enumeration features

1. Using enum to define an enumeration class by default inherits the Java.lang.Enum class instead of inheriting the object class. Where the Java.lang.Enum class implements the Java.lang.Serializable and java.lang.Comparable two interfaces.

2. The constructor for an enumeration class can only use the private access modifier, and if its constructor's access control is omitted, the private adornment is used by default.

3. All instances of an enumeration class must be explicitly listed in the enumeration class, or the enumeration class will never produce an instance. When these instances are listed, the public static final decoration is automatically added without the programmer being explicitly added.

4. A variable value of an enumeration type can store an enumerated value or a null value given in the type declaration, and NULL indicates that the variable has no value set.

5. If you compare two enum-Type de values, never call equals and use "= =" directly.

6. Enumerations can also add methods and properties like normal classes, and you can add static and non-static properties or methods to it.

For example:

 Public enumGender {MALE ("Male"), FEMALE ("female"); PrivateString name; PrivateGender (String name) { This. Name =name; }     PublicString GetName () {return  This. Name; }     Publicstring toString () {string name=NULL; Switch( This) {         CaseMale:name= "Male";  Break;  CaseFemale:name= "female";  Break; }        returnname; }}

java--enum enum

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.