Java Enum Enumeration

Source: Internet
Author: User

Before enumeration:

Before enumeration, we want to list some related constants in the following way:

 
Interface actioninterface {public static final int right = 0; public static final int left = 1 ;}

Then use these constants in a method similar to the following:

 
Public void playwithinterface (INT num) {Switch (Num) {Case actioninterface. right: system. out. println ("right"); break; Case actioninterface. left: system. out. println ("Left"); break; default: system. out. println ("default ");}}

I don't know if you have noticed it,In this case, remember that these constants are int type constants in Java, which means this method can accept any int type value, even if it does not match all constants set in the previous interface. Therefore, you need to check the upper and lower bounds. When an invalid value occurs, you can only rely on default in the switch or other processing methods. If you add another constant later, then you must change many associatedCodeUpper and lower bounds inProgramTo accept the new value.

With enumeration:

You should always use enumeration in this case, for example:

Enum actionenum {right, left}

Then use them in a method similar to the following:

 
Public void playwithenum (actionenum) {Switch (actionenum) {case right: system. out. println ("right"); break; case left: system. out. println ("Left"); break; default: system. out. println ("default ");}}

In this way, you don't have to worry a lot about checking the previous problems. Even if you add some related variables after the requirement is modified, you only need to modify actionenum, no other code needs to be modified.

Note that in the preceding switch, we use case right... instead of case actionenum. Right (this will cause an error) becauseThe enumerated value overrides tostring (), that is, the enumerated variable value does not have a prefix.

As mentioned above, the advantages of enumeration types may not be fully displayed, but we often use them. Let's take a look at enumeration as much as possible:

Package COM. java; public class enumdemo {/*** simplest enumeration example **/Public Enum simpleenum {Up, down}/*** enumeration example with properties and Methods * Note: enumeration itself is also a class. You can add static or non-static attributes or methods for it like a general class * But the enumeration list must be written at the beginning; otherwise, an error occurs during compilation. For example, in this example, up, down **/Public Enum enumwithfuncandattr {Up, down; Private Static final string description = "this is an example of method and attribute enumeration "; public String getdescription () {return description;}/*** enumeration example with constructor * Note: enumeration can have constructor, which is assigned with a value through parentheses. If no value is assigned, the constructor cannot be compiled; otherwise, an error occurs. * The constructor can only be private or default. It cannot be public or protected. Otherwise, an error occurs. This ensures that the customer Code cannot create an Enum instance **/Public Enum enumwithconstructor {up ("hello"), down ("Java"); private final string value; string getvalue () {return value;} enumwithconstructor (string value) {This. value = value ;}} public static void main (string [] ARGs) {system. out. println (simpleenum. values (). length); system. out. println (simpleenum. UP); system. out. println (simpleenum. valueof ("up"); For (enumwithconstructor item: enumwithconstructor. values () {system. out. println (item + "" + item. getvalue ();} system. out. println (simpleenum. up. ordinal ());}}

Summary:

1. Enumeration itself is a class.

2.It cannot have a public constructor. This ensures that the customer Code cannot create an Enum instance.

3.. All enumerated values are public static final. Note that this is only for enumeration values. We can define other types of non-enumeration variables like variables defined in common classes. These variables can use any modifier you want.

4.By default, Enum implements the java. Lang. Comparable interface.

5.Enum overwrites the tostring method. Therefore, if we call color. Blue. tostring (), the "blue" string is returned by default ".

6.Enum provides a valueof method, which corresponds to the tostring method. Calling valueof ("blue") will return color. Blue. Therefore, we must note this when rewriting the tostring method. Generally, the valueof method should be overwritten.

7.Enum also provides the values method, which allows you to easily traverse all enumerated values.

8.Enum also has an oridinal method, which returns the order of the enumerated values in the enumerated classes.

 

 

 

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.