Java Object-oriented note 9

Source: Internet
Author: User

If the object of a class is finite and fixed, such as the seasonal class, it has only 4 objects, such as a finite and fixed class, which we have in Java


is called an enumeration class.


The enumeration class is new to Java 5, which uses the enum keyword to define the enumeration class, which is a special class of clocks that can have its own field,


method, you can implement one or more interfaces, or you can define your own constructors. A maximum of one public access can be defined in a Java source file


The enumeration class for permissions. The following differences exist between enum classes and ordinary classes:


1. An enumeration class can implement one or more interfaces, and an enum class defined with an enum inherits the Java.lang.Enum class by default and does not inherit the object class.


Where the Java.lang.Enum class implements the Java.lang.Serializable and java.lang.Comparable two interfaces.


2. enum-defined, non-abstract enumerations use the final adornment by default, so the enumeration class cannot derive subclasses.


3. The constructor of 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 if


Enforces the specified access control, only the private modifier can be specified.


4. All instances of an enumeration class must be explicitly listed in the first row of the enumeration class, or the enumeration class will never generate an instance. When you list these instances,


The public static final decoration is automatically added without the program being explicitly added.


The following is an example of an enumeration class:

public enum Gender {MALE ("male"), FEMALE ("female");p rivate 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:

Woman



Reprint Please specify source: http://blog.csdn.net/hai_qing_xu_kong/article/details/43889967 Emotional Control _  

Java Object-oriented note 9

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.