JAVA enumeration class

Source: Internet
Author: User

JAVA enumeration class
I. enumeration is a standard that standardizes the form of parameters, in this way, you do not need to consider the Type mismatch and explicitly Replace the fuzzy conceptual enumeration that the int type parameter may bring. It is 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 variables, define its own methods, and implement one or more interfaces. When declaring an enum type, we should note that the enum type has the following features. 1. This ensures that the customer Code cannot create an enum instance. 2 .. Note that this is only for enumeration values. We can define other types of non-enumeration variables like variables defined in common classes. These variables can use any modifier you want. 3. Enum implements the java. lang. Comparable interface by default. 4. enum overwrites the toString method, so if we call Color. blue. toString () returns the string "Blue" by default ". 5. enum provides a valueOf method, which corresponds to the toString method. Calling valueOf ("Blue") will return Color. Blue. Therefore, we need to note this when rewriting the toString method. For example, we should override the valueOf method. 6. Enum also provides the values method, which allows you to easily traverse all enumeration values. 7. enum also has an oridinal method. This method returns the order of the enumerated values in the enumerated classes. This order depends on the order in which the enumerated values are declared. Here, Color. red. ordinal () returns 0. Ii. Usage of enumeration 1: Before JDK1.5, we define constants as publicstaticfianl ..... Now, with enumeration, You can group related constants into one Enumeration type, and enumeration provides more methods than constants. Java code public enum Color {RED, GREEN, BLANK, YELLOW} usage 2: switch statements before switch JDK1.6 only support int, char, enum type, use enumeration, it makes our code more readable. Java code: enum Signal {GREEN, YELLOW, RED} public class TrafficLight {Signal color = Signal. RED; public void change () {switch (color) {case RED: color = Signal. GREEN; break; case YELLOW: color = Signal. RED; break; case GREEN: color = Signal. YELLOW; break ;}}usage 3: Add a new method to enumeration. If you want to customize your own method, you must add a semicolon at the end of the enum instance sequence. In addition, Java requires that the enum instance be defined first. Java code public enum Color {RED ("RED", 1), GREEN ("GREEN", 2), BLANK ("white", 3 ), YELLO ("***", 4); // member variable private String name; private int index; // constructor private Color (String name, int index) {this. name = name; this. index = index;} // common method public static String getName (int index) {for (Color c: Color. values () {if (c. getIndex () = index) {return c. name ;}} return null;} // get set Method public String GetName () {return name;} public void setName (String name) {this. name = name;} public int getIndex () {return index;} public void setIndex (int index) {this. index = index ;}}usage 4: overwrite the enumeration method. An example of overwrite the toString () method is given below. Java code public enum Color {RED ("RED", 1), GREEN ("GREEN", 2), BLANK ("white", 3 ), YELLO ("***", 4); // member variable private String name; private int index; // constructor private Color (String name, int index) {this. name = name; this. index = index;} // Override method @ Override public String toString () {return this. index + "_" + this. name ;}} usage 5: All enumeration operations are inherited from java. lang. enum class. Because Java does not support multi-inheritance, enumeration objects cannot inherit other classes. Java code public interface Behaviour {void print (); String getInfo ();} public enum Color implements Behaviour {RED ("RED", 1), GREEN ("GREEN ", 2), BLANK ("white", 3), YELLO ("***", 4); // member variable private String name; private int index; // constructor private Color (String name, int index) {this. name = name; this. index = index;} // interface method @ Override public String getInfo () {return this. name;} // interface method @ Override public void Print () {System. out. println (this. index + ":" + this. name) ;}} usage 6: organize and enumerate Java code public interface Food {enum Coffee implements Food {BLACK_COFFEE, DECAF_COFFEE, LATTE, CAPPUCCINO} enum Dessert implements Food {FRUIT, CAKE, GELATO} 7 usage 7: java for enumeration sets. util. enumSet and java. util. enumMap is an enumeration set. EnumSet ensures that the elements in the set are not repeated. The key in EnumMap is of the enum type, and the value can be of any type. The usage of these two sets is not described here. You can refer to the JDK documentation.

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.