Java Enum type

Source: Internet
Author: User

(1) about Java Enum: people who have studied C/C ++ and other languages should be slightly aware of the enum type. Jdk1.5 does not have the enum type before. At that time, it is generally replaced by an interface constant. With javaenum, you can more closely represent such constants.

(2) how to use Java enum.

Simple usage: javaenum is generally used to represent a group of common constants. For example:
Gender:
Public Enum sexenum {
Male, female;
}
Color:
Public Enum color {
Red, blue, green, black;
}
The values in the enumerated objects must be unique. You can use the enum type name to directly reference this constant, such as sexenum. Male, color. Red.
Complex usage: Java provides some built-in methods for enumeration types, and enumeration constants can also have their own methods to easily traverse enumeration objects.

(3) Instances

Enum file weekday. Java:

Public Enum weekday {Mon ("Monday"), // both of them should be string type Tue ("Tuesday"), wed ("Wednesday"), Thu ("Thursday "), fri ("Friday"), sat ("Saturday"), Sun ("Sunday"); private final string day; private weekday (string Day) {// Enum class constructor this. day = day;} public static void printday (int I) {Switch (I) {Case 1: system. out. println (weekday. mon); break; Case 2: system. out. println (weekday. tue); break; Case 3: system. out. println (weekda Y. wed); break; Case 4: system. out. println (weekday. thu); break; Case 5: system. out. println (weekday. fri); break; Case 6: system. out. println (weekday. sat); break; Case 7: system. out. println (weekday. sun); break; // The output is actually the value of Enum, rather than the string value corresponding to the member default: system. out. println ("wrong number! ") ;}} Public String getday () {return day ;}}

Test File weekdaytest. Java:

Public class weekdaytest {public static void main (string [] ARGs) {// todo auto-generated method stubfor (weekday day: weekday. values () {// cyclic count system. out. println (day + "===>" + day. getday (); // string value corresponding to Enum} weekday. printday (5 );}}

The execution result is:

Mon ===> Monday
Tue ===> Tuesday
Wed ===> Wednesday
Thu ===> Thursday
Fri ==> Friday
Sat ==> Saturday
Sun ==> Sunday
Fri
(4) Java Enum nature

Although the syntax structure of the Java Enum type is different from that of the Java class, a class file is generated after compilation by the compiler. This class file is decompiled to show that a class is actually generated, which inherits java. Lang. enum.

Public final class weekday extends java. Lang. Enum {
Public static final weekday mon;
Public static final weekday Tue;
Public static final weekday wed;
Public static final weekday Thu;
Public static final weekday Fri;
Public static final weekday sat;
Public static final weekday sun;
Static {};
Public static void printday (INT );
Public java. Lang. String getday ();
Public static weekday [] values ();
Public static weekday valueof (Java. Lang. String );
}

Therefore, the enum type is actually implemented using Java classes and has no new features, except that the Java compiler helps us parse and compile the syntax.

(5) Java ordinal Functions

The enum class provides an ordinal () method to return the ordinal number of the enumerated object.

Private Static Enum gene_actions {
Action0,
Action1,
Action2,
Action3,

}
Public final static int invalid_action = gene_actions.action0.ordinal ();
Public final static int smart_handfree_action = gene_actions.action1.ordinal ();
Public final static int anti_eavesdrop_action = gene_actions.action2.ordinal ();
Public final static int direct_call_action = gene_actions.action3.ordinal ();

The obtained ordinal value is 0, 1, 2, 3.

Http://blog.sina.com.cn/s/blog_60452f7d0100it3h.html ()

Http://malinhu.blog.51cto.com/3568860/701927 ()

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.