Common uses of Java enumeration 7

Source: Internet
Author: User

DK1.5 introduces a new type--enumeration. In Java, although it is a "small" function, but to my development has brought "big" convenience.

Usage One: Constants

before JDK1.5, we define constants that are: publicstaticfianl ..... Now, with enumerations, you can group related constants into an enumeration type, and enumerations provide more methods than constants.

 Public enum Color {    RED, GREEN, BLANK, YELLOW  
Usage Two: Switch

The switch statement before JDK1.6 only supports int,char,enum types, and using enumerations can make our code more readable.

    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; }          }      }  
Usage Three: Add a new method to the enumeration

If you intend to customize your own method, you must add a semicolon at the end of the enum instance sequence. And Java requires that an enum instance be defined first .

     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; }      }  
Usage Four: Methods for overriding enumerations

An example of the ToString () method overlay is given below.

 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; }}
Usage Five: Implement Interface

All enumerations are inherited from the Java.lang.Enum class. Because Java does not support multiple inheritance, enumeration objects can no longer inherit from other classes.

     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); }      }  
Usage VI: Organizing enumerations using interfaces
     Public Interface Food {          enumimplements  food{              Black_coffee,decaf_coffee,latte,cappuccino          }           enum Implements food{              FRUIT, CAKE, GELATO          }      }  
Usage Seven: About the use of enumeration collections

Java.util.EnumSet and Java.util.EnumMap are two enumeration collections. Enumset guarantees that the elements in the collection are not duplicated; The key in Enummap is the enum type, and value can be any type. The use of this two collection is not mentioned here, you can refer to the JDK documentation.

For the implementation details and principles of enumerations, please refer to:

Reference: "Thinkinginjava" fourth edition

http://softbeta.iteye.com/blog/1185573

Common uses of Java enumeration 7

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.