In some cases, the object of a class is limited and fixed, such as the seasonal class, which has only 4 objects. This example of a finite and fixed class, in Java, is called an enumeration class.
In the early days, the enumeration classes might be represented directly using simple static constants, for example:
public static final int season_spring = 1;public static final int season_summer = 2;public static final int season_fail = 3;public static final int season_winter = 4;
This definition is straightforward, but there are some problems:
Type is unsafe: because a season is used as an int integer, for example an addition operation.
There is no namespace: such as the season prefix, otherwise it will be confused with others.
The meaning of the printout is ambiguous: for example, output season_spring, the output is actually 1.
But enumerations do have meaning, so they can be implemented in the early stages by defining classes.
Hides the constructor through private.
All possible instances of this class are saved using the public static final modified class variable.
If necessary, you can provide some static methods that allow other programs to get an instance of the match based on the parameters.
Using an enumeration class can make your program more robust and avoid the randomness of creating objects.
However, it is cumbersome to implement enumerations by defining classes to implement them, and Java has added support for enumeration classes since JDK1.5.
Entry:
Java5 adds an enum keyword (which is the same as the class, interface keyword) to define an enumeration class, which is a special class that can have its own member variables, methods, implement one or more interfaces, or define its own constructor. A Java source file can define up to one enumeration class with a public access permission and the same name.
However, the enumeration class is not a normal class after all, it differs from the normal class in the following:
An enumeration class can implement one or more interfaces, and an enumeration class defined with an enum inherits the Java.lang.Enum class by default, rather than inheriting the object class by default, so the enumeration class cannot display inheritance from other parent classes. Where the Java.lang.Enum class implements the java.lang.Serializable and java.lang.Comparable interfaces.
Enum classes that use enum definitions, non-abstractions, by default use the final adornment, so the enumeration class cannot derive subclasses.
The constructor for an enumeration class can only use the private access control, and if the constructor's access control is omitted, the private adornment is used by default, and only the private adornment is specified if the access control is enforced.
All instances of an enumeration class must appear listed in the first row of the enumeration class, or the enumeration class will never produce an instance. Listing these instances, the system automatically adds a public static final decoration.
The enumeration class provides a value () method by default, which is a convenient way to iterate through all of the enumeration values.
An instance of an enumeration class can only be an enumeration value, not an enumeration class object created arbitrarily by new
This article is from the "where No Play" blog, please be sure to keep this source http://liaofan.blog.51cto.com/12295212/1917025
Java Enum class