Java study notes 9, java Study Notes

Source: Internet
Author: User

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 _

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.