Functions and characteristics of enumerations

Source: Internet
Author: User


I. Introduction to enumerations here Reference: http://blog.sina.com.cn/s/blog_4adc4b090101dtxp.html
An enumeration is a specification that regulates the form of a parameter, so that it does not take into account the type mismatch and explicitly replaces the fuzzy concept that the int parameter may bring, which prints out its own name and order (that is, the ordinal number in the enumeration).enumerations are like a class and an array.

As a new keyword introduced by Sun, Enum looks like a special class, it can also have its own variable, can define its own method, can implement one or more interfaces. When we declare an enum type, we should be aware of some of the following characteristics of the enum type:

1. It cannot have a constructor for public, this ensures that the customer code has no way to create an instance of the enum.

2.all enumeration values are public, static, final。 Note that this is only for enumeration values, and we can define any other type of non-enumeration variable as you would define a variable in a normal class, which can use any modifier you want to use.

3. Enum implements the Java.lang.Comparable interface by default.

4. The enum contains the ToString method, so if we call Color.Blue.toString (), the string "Blue" is returned by default.

5. Enum provides a valueof method that corresponds to the ToString method. Calling valueof ("Blue") will return Color.Blue. So we have to be aware of this when we rewrite the ToString method, which should be a relative rewrite of the valueof method.

6. Enum also provides the values method, which allows you to easily iterate through all of the enumeration values.

7. Enum also has a oridinal method that returns the order of enumeration values in the enumeration class, which depends on the order in which the enumeration values are declared, where Color.Red.ordinal () returns 0.

Second, the following describes the principle of enumeration:
In fact, the enumeration principle is implemented with internal classes, of course, there are other methods inside the class, similar to the following:

public class Weekday {public final static Weekday Mon=new Weekday () {public Weekday nexday () {return Tue;}}; Public final static Weekday Tue=new Weekday () {public Weekday nexday () {return Wen;}}; Public final static Weekday Wen=new Weekday () {public Weekday nexday () {return Tue;}};

  

Functions and characteristics of enumerations

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.