Java Enumeration Class (recent reading notes to organize, review the use, save a file

Source: Internet
Author: User

In some cases, the object of a class is limited and fixed.

such as the seasons on Earth, there are Four Seasons , that is, four objects .

This example is a finite and fixed class , known as an enumeration class .

In the early days, a simple static variable was used to represent the enumeration directly by defining the class.

such as public static final int season_spring=1; Note: The content on the left changes the name and X4

However, there are many inconveniences and defects in this way. (such as type safety and easy to confuse, later, it evolved ...)

To change body:

A new keyword was added to the Java5,enum(same status as Class,interface)

It is a special class that can have its own member variables, methods, can implement one or more interfaces, or you can define your own constructors.

Compare:

public enum season{

Spring,summer,fall,winter;

}

Isn't it very concise?

But it is not an ordinary class, it differs from the ordinary class:

1 enumeration classes can implement one and more interfaces, An enumeration class defined with an enum inherits the Java.lang.Enum class by default, rather than inheriting the object class, so the enumeration class cannot explicitly inherit other parent classes, where the Java.lang.Enum class implements Java.lang.Serializable and Java.lang.Compar Able two interfaces.

2 enum classes that use enum definitions, non-abstractions, will use the final adornment by default, so the enumeration class cannot derive subclasses.

3 constructors for enum classes can only use the private access control

4 Enum class All instances must be explicitly listed in the first row, or the instance can never be generated.

the class by default provides a values () method, which is a convenient way to iterate through all the enumeration values.

Methods provided in the Java.lang.Enum class

public enum Gender

{

Male,female;

Defining an instance variable of public adornment

public String name;

}

public class Gendertest

{

Main method (here)

Gets the enum value of the Enum class through the valueof () method of the enum

Gender g=enum.valueof (Gender.class, "FEMALE");

G.name= "female";

The name instance variable that accesses the enumeration value directly

System.out.println ("G" + "representative:" +g.name);

}

}

You can make some improvements by setting the string name to a private property, and then adding a method that uses the switch control to implement a public SetName method to avoid confusion.

For the time being here, a little progress every day. Learning up

Java Enumeration Class (recent reading notes to organize, review the use, save a file

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.