This code is often seen in the Code:
Public static final int app_start = 1;
Public static final int app_pause = 0;
Public static final int app_stop = 2;
Public static final int play_start = 1;
Public static final int play_pause = 0;
Public static final int play_stop = 2;
This method is called the int enumeration mode, which has many shortcomings, but is often written like this. It does not help in type security and ease of use
If you pass the app _ to the play _ method, the compiler will not receive a warning, and the = operation will be used to compare the app _ and play _, or even worse:
Int I = (app_start-play_start)/play_start;
Using int enumeration is a compilation constant. If the value related to the int constant changes, the client can only achieve the effect after re-compilation.
How can we avoid this defect? The Enum enumeration solution was provided in Java:
Public Enum app {start, stop, pause };
Public Enum play {start, stop, pause };
On the surface, there are no differences, but this is not the case. The Java Enumeration type is fully functional, and Java enumeration is essentially an int value.
Enumeration provides type security during compilation.If an app is declared as the parameter type, it is guaranteed that any non-empty object uploaded to this parameter must
It is one of the three valid app attributes and does not reference the play attributes.
Multiple enumerations of constants with the same name can coexist in one system.. Because each type has its own namespace
For example, there is no conflict between app. Start and play. Start.
You can also add any methods and fields to the enumeration type.You can use an appropriate method to enhance the enumeration type, or use it as a set of enumeration constants.
And becomes full-featured abstraction over time
In short, compared with int constants, the advantages of enumeration types are self-evident. enumeration types are much easier to read, safer, and more powerful.