Java study notes 9, java Study Notes
If the object of a class is finite and fixed, such as the seasonal class, it has only four objects. For example, if an instance is finite and fixed
Is called an enumeration class.
Enumeration classes are newly added in Java 5. The enum keyword is used to define enumeration classes. Enumeration classes are special classes with the same Field,
You can implement one or more interfaces or define your own constructor. Only one public access can be defined in a Java source file.
Permission enumeration class. Enumeration classes and common classes have the following differences:
1. An enumeration class can implement one or more interfaces. The enum-defined enumeration class inherits the java. lang. Enum class by default, rather than the Object class.
The java. lang. Enum class implements java. lang. Serializable and java. lang. Comparable interfaces.
2. enum-defined and non-abstract enumeration are modified using final by default. Therefore, enumeration classes cannot be derived from subclasses.
3. the constructor of the enumeration class can only use the private access controller. If the constructor's access controller is omitted, the private modifier is used by default.
You can only specify the private modifier if the access control operator is specified.
4. All instances of the enumeration class must be explicitly listed in the first row of the enumeration class. Otherwise, this enumeration class will never produce instances. When listing these instances,
The system automatically adds public static final modification without explicitly adding a program.
The following is an example of an enumeration class:
Public enum Gender {MALE ("MALE"), FEMALE ("FEMALE"); private final String sex; private Gender (String sex) {this. sex = sex;} public String getSex () {return this. sex ;}}
public class Main {public static void main(String[] args) {System.out.println(Gender.FEMALE.getSex());}}
Output result:
Female
Reprinted please indicate the source: http://blog.csdn.net/hai_qing_xu_kong/article/details/43889967 sentiment control _