Java Enumeration Classes

Source: Internet
Author: User

I. the role of enumerations

An enumeration is a specification that regulates the form of parameters, so that you do not have to consider the type mismatch and explicitly replace the fuzzy concept that the int parameter might bring   enumerations 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, 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 characteristics of the enum type. 1. , this ensures that the customer code has no way to create an instance of the enum.  2. Note that this is only for enumeration values, and we can define other non-enumeration variables of any type, as defined in the normal class   variables, which can be used with any modifier you want.  3. Enum implements the Java.lang.Comparable interface by default.  4. The enum contains the ToString method, so if we call Color.Blue.toString (), we return the string "Blue" 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. The usage of enumeration one: Constants before JDK1.5, we define constants are: Publicstaticfianl ...  Now, with enumerations, you can group related constants into an enumeration type, and enumerations provide more methods than constants.  Java code public enum color {Red, green, blank, yellow} usage two: switch  The switch statement before JDK1.6 only supports int,char,enum types, and using enumerations can make 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 }}} Use three: Add a new method to the enumeration if you intend to customize your own method, you must add a semicolon at the end of the enum instance sequence.  And Java requires that an 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;    Construction Method 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;  }} Use four: Override the enumeration method below gives an example of the ToString () method overlay. Java Code public enum color {Red ("Red",  1),  green ("green",  2),  blank ("white",  3),   yello ("* * *",  4);    Member Variable private string name;  private int index;    Construction Method Private color (string name, int index)  {this.name = name;  this.index = index;  }//Coverage method @Override public string tostring ()  {return this.index+ "_" +this.name; }} Use five: Implement an interface all enumerations inherit from the Java.lang.Enum class.  Because Java does not support multiple inheritance, enumeration objects can no longer inherit from 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;    Construction Method 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 VI: Enumerate Java code using interface organization public interface food {enum coffee implements food{BLACK_COF  Fee,decaf_coffee,latte,cappuccino} enum dessert implements food{FRUIT, CAKE, GELATO}}7 Usage Seven: The use of Java.util.EnumSet and JAVA.UTIL.ENUMMAP for enumeration collections is two enumeration collections. Enumset guarantees that the elements in the collection are not duplicated; The key in Enummap is the enum type, and value can be any type. The use of this two collection is not mentioned here, you can refer to the JDK documentation.

This article is from the "Listen to the Rain Hope Eternal" blog, please be sure to keep this source http://yongguang.blog.51cto.com/9153118/1709598

Java Enumeration Classes

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.