Using enumerations to represent data dictionaries in Java development

Source: Internet
Author: User

I. Using enumerations to represent data dictionaries

1. Code:

 Packagecom.inspire.jdk.caculate;/*** Created by yaming * Use enumerations to represent constant data fields*/ Public enumStateenum {
   // using construction methods to create objects , each of which is an instance of stateenum SUCCESS (
1, "Success"), FAILED (0, "failed"); Private intState ; PrivateString StateInfo; Public intgetState () {returnState ; } PublicString Getstateinfo () {returnStateInfo; }Stateenum (intState , String StateInfo) { This. State =State ; This. StateInfo =StateInfo; } /*** Find corresponding Stateenum according to index of INT type *@paramIndex *@return */ Public StaticStateenum stateof (intindex) { for(Stateenum state:values ()) {if(state.getstate () = =index) { returnState ; } } return NULL; } /*** Find the value of the corresponding string according to the index of type int *@paramIndex *@return */ Public StaticString Valuesof (intindex) { for(Stateenum state:values ()) {if(state.getstate () = =index) { returnState.getstateinfo (); } } return"NULL"; }}

The enumeration class (enum) in 2.java is the same as the normal class (class) in Java, and only a series of sample variables are pre-defined

such as:  Public enum Color {//  each variable is a sample of color RED, YELLOW, BLUE;}

2.1 It is possible to write sample variables, such as Red,yellow,blue, directly within the enumeration.
The 2.2 enumeration has some naturally obtained member methods, the Color.values () method

2.3 An enumeration class is similar to a normal class on the basis that an enumeration can also contain constructors

3. Summary:

Enumeration limits the number and likelihood of instance variables of that type, because an instance variable of an enumeration determines when the enumeration is defined

Using enumerations to represent data dictionaries in Java development

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.