Java Constant Enumeration method

Source: Internet
Author: User

Stop obsessing about what's on the ground, what's the technology, learn to think, change the mindset

I. Status of Enumeration

In your usual coding you will find that many design patterns are not the same type of figure, which makes the enumeration of the indispensable status (type)

Second, constant

We often use constants to define some error codes, etc.

If it's a constant, your code might be like this.

1  Public Final Static classsysmsg{2        Public  Static FinalStatus_success_code= "0";3        Public  Static FinalStatus_failure_code= "-1";4        Public  StaticFIANL sys_success_code= "1000";5        Public  Static FinalSys_sign_code= "1001";6        Public  StaticFIANL sys_sing_msg= "Illegal Request"; 7}

This is the case with enumerations.

1  Public enumenumt{2SUCCESS (1, "Operation succeeded"), FAILURE (1, "Operation failed"), FAILURE (4, "Signature failed");3         PrivateString name;4         Private  intindex; 5         PrivateENUMT (String name,intindex) {6                This. name=name;7                This. index=index;8        }9          PublicString GetName (intindex) {Ten                  for(Enumt c:enumt.values ()) { One                       if(c.getindex==index) { A                               returnc.name; -                       } -                  }   the                        return Nuljul; -         } -          PublicString GetName () { -             returnname; +         } -  +}

Iii. creating a String constant enumeration

 Public   enum   enumtask{      INITIAL ("Initialize"), processing ("in process"), processed ("Processing done"), FAILURE ("Processing failed");          Private String  status;       Enumtask (String status) {                this. status=status;       }           Public String  getStatus () {             return   status;       }    }

Four, reverse lookup method

I believe you see the above example has some insight

Reverse lookup ideas Define a map static code block that uses traverse values () to provide an external calling method using the value as a key setting map

It would be useful to use enumerations skillfully in the design.

Java Const Enumeration Method

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.