Enumeration is a standardized form of parameters, so that you can explicitly replace the fuzzy concepts that int-type parameters may cause. Enumeration is like a class and an array without considering type mismatch.
As a new keyword introduced by Sun, Enum looks like a special class. It can also have its own variables, define its own methods, and implement one or more interfaces. When we declare an enum type, we should notice that the enum type has some characteristics as follows.
1. It cannot have a public constructor, and doing so can ensure that the client code cannot create a new instance of enum.
2. All enum values are public , static , final. Note that this is only for enum values. We can define any other non-enumer variables of any type just like defining variables in ordinary classes. These variables can be made with any modifier you want.
3. Enum implements the java.lang.Comparable interface by default.
4. Enum overrides the toString method, so if we call Color.Blue.toString(), we return the string "Blue" by default.
5. Enum provides a valueOf method, which corresponds to the toString method. Calling valueOf("Blue") will return Color.Blue. Therefore, we should pay attention to this when rewriting the toString method ourselves. In other words, we should rewrite the valueOf method accordingly.
6. Enum also provides the values method, which allows you to easily traverse all enum values.
7. Enum also has an oridinal method, which returns the order of enum values in the enum class. This order depends on the order of enum value declaration. Here Color.Red.ordinal() returns 0.
After understanding these basic features, let's see how to use them.
1. Traversing all enum values. Knowing that there are values methods, we can use the ForEach loop to traverse the enum values with a familiar way.
for (Color c: Color.values()) System.out.println("find value:" + c);2. Define methods and variables in enum. For example, we can add a method to Color to randomly return a color.
public enum Color { Red, Green, Blue; private static int number = Color.values().length ; public static Color getRandomColor(){ long random = System.currentTimeMillis() % number; switch ((int) random){ case 0: return Color.Red; case 1: return Color.Green; case 2: return Color.Blue; default : return Color.Red; } } }It can be seen that there is no difference between defining variables and methods in enum types and defining methods and variables in ordinary classes. The only thing to note is that variable and method definitions must be placed after all enum value definitions, otherwise the compiler will give an error.
3. Override toString, valueOf method
We already know that enum provides methods such as toString, valueOf, etc. Many times we need to override the default toString method, so how do we do enum? In fact, this is no different from overriding a toString method with an ordinary class.
…. public String toString(){ switch (this){ case Red: return "Color.Red"; case Green: return "Color.Green"; case Blue: return "Color.Blue"; default: return "Unknow Color"; } } ….At this time, we can see that the one printed out with the previous traversal code is
Color.Red Color.Green Color.Blue
Instead
Red Green Blue.
You can see that toString is indeed overwritten. Generally speaking, when overwriting toString, we should also overwrite the valueOf method to maintain their consistency.
4. Using constructors
Although enum cannot have a public constructor, we can still define the private constructor and use it inside the enum. Let's use the Color example.
public enum Color { Red("This is Red"), Green("This is Green"), Blue("This is Blue"); private String desc; Color(String desc){ this.desc = } public String getDesc(){ return this.desc; } }Here we provide an explanation information for each color, and then define a constructor to accept this explanation information.
It should be noted that the constructor cannot be public or protected, so as to ensure that the constructor can only be used internally, and the client code cannot new an instance of the enum value. This is also completely reasonable, because we know that enum values are just constants of public static final.
5. Implement specific interfaces
We already know that enum can define variables and methods. It needs to implement an interface and implement an interface like ordinary class. I won't give an example here.
6. Define your own method of enumeration values.
We saw earlier that some methods can be defined for enum, but in fact we can even define methods for each enum value. In this way, our previous example of overriding toString can be rewritten like this.
public enum Color { Red { public String toString(){ return "Color.Red"; } }, Green { public String toString(){ return "Color.Green"; } }, Blue{ public String toString(){ return "Color.Blue"; } }; }Logically speaking, this is clearer than the original toString method that provides a "global" one.
In general, enum, as a completely new type, hopes to help programmers write codes that are simpler and easier to understand.
People generally don’t need to use some advanced features of enum, otherwise it will go against the simple and easy-to-understand original intention.
The above article briefly talks about the role and benefits of Jave enumeration is all the content I share with you. I hope it can give you a reference and I hope you can support Wulin.com more.