Java enumeration types

Source: Internet
Author: User

What is the essence of enumerations?

Enumerations inherit the Enum class, each member of an enumeration type is declared as public static final, each of which is a single instance of that type, and each object in the enumeration is an enumeration of objects that are currently instantiated by the current enumeration, and each of these objects can be called

Methods in Enum:

A.ordinal () is used to return the order of enumerated values in the enumeration class.

B.compareto () compares the order of two enumeration values, returning the difference in order.

C.values (), a static method that returns an array that contains a full enumeration value.

D.tostring (), returns the name of the enumeration constant

E.valueof () that returns an enumeration constant that specifies an enumerated type that is not made.

Let's look at a simple example:

enumfruitenum {APPLE (1), ORANGE (2);//call the constructor to construct an enumeration item    Private intValue = 0; PrivateFruitenum (intValue) {//must be private, or compile error         This. Value =value; }     Public StaticFruitenum ValueOf (intValue) {//handwritten conversion functions from int to enum        Switch(value) { Case1:            returnAPPLE;  Case2:            returnORANGE; default:            return NULL; }    }     Public intvalue () {return  This. Value; }} Public Static voidMain (string[] args) {System.out.println (FruitEnum.APPLE.value ()); //1System.out.println (FruitEnum.ORANGE.value ());//2System.out.println (fruitenum.valueof (1));//Fruitenum.appleSystem.out.println (fruitenum.valueof (2));//Fruitenum.orange}

Here's how to use the various versions on the network:
Usage One: Constants

 Public enum Color {    RED, GREEN, BLANK, YELLOW  
View Code

Usage Two: Switch

enumSignal {GREEN, YELLOW, RED} Public classtrafficlight {Signal color=signal.red;  Public voidChange () {Switch(color) { CaseRed:color=Signal.green;  Break;  CaseYellow:color=signal.red;  Break;  CaseGreen:color=Signal.yellow;  Break; }        }    }
View Code

Usage Three: Add a new method to the enumeration

 Public enumColor {RED ("Red", 1), Green ("green", 2), BLANK ("White", 3), Yello ("Yellow", 4); //member Variables        PrivateString name; Private intindex; //Construction Method        PrivateColor (String name,intindex) {             This. Name =name;  This. index =index; }        //Common Methods         Public StaticString GetName (intindex) {             for(Color c:color.values ()) {if(C.getindex () = =index) {                    returnC.name; }            }            return NULL; }        //Get Set Method         PublicString GetName () {returnname; }         Public voidsetName (String name) { This. Name =name; }         Public intGetIndex () {returnindex; }         Public voidSetindex (intindex) {             This. index =index; }    }
View Code

Usage Four: Methods for overriding enumerations

 Public classTest { Public enumColor {RED ("Red", 1), Green ("green", 2), BLANK ("White", 3), Yello ("Yellow", 4); //member Variables        PrivateString name; Private intindex; //Construction Method        PrivateColor (String name,intindex) {             This. Name =name;  This. index =index; }        //override Method@Override PublicString toString () {return  This. Index + "_" + This. Name; }    }     Public Static voidMain (string[] args) {System.out.println (Color.RED.toString ()); }}
View Code

Usage Five: Implement Interface

 Public InterfaceBehaviour {voidprint ();    String GetInfo (); }     Public enumColorImplementsbehaviour {RED ("Red", 1), Green ("green", 2), BLANK ("White", 3), Yello ("Yellow", 4); //member Variables        PrivateString name; Private intindex; //Construction Method        PrivateColor (String name,intindex) {             This. Name =name;  This. index =index; }        //interface Method@Override PublicString GetInfo () {return  This. Name; }        //interface Method@Override Public voidprint () {System.out.println ( This. Index + ":" + This. Name); }    }
View Code

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.