Enumerated types JDK1.5

  Abstract: JDK1.5 enumerated types 

  </ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width = "100%" border = "0" cellspacing = "0" cellpadding = " 0 "> <tr> <td width="282" height="86" align="center" valign="top"> </ td> <td width="402" valign="top"> 

  Enum the most simple form, similar to, 
  (Public enum Color 
Red,
Green,
Blue;
  ) 

  This article us to explain in detail the characteristics of enum.    Enum as Sun introduced a new keyword, seemed like a special class, it can also have its own variables, can define their own methods, can achieve one or more interfaces.    When we declare a enum type, we should note that the enum type features are as follows. 

  1.    It does not have a public constructor function, it can not guarantee that the new code, a enum examples. 

  2.    All values are enumerated public, static, and the final.    Attention to the fact that only focuses on the enumeration values, and in general we can define variables such as definition of any other type of non-enumeration variables, these variables can be anything you want to use the Xiuchifu. 

  </ Td> </ tr> </ table> 

  3.    Enum default realized java.lang.Comparable interface. 

  4.    Enum contains a review of the toString method, so if we call Color.Blue.toString () to return to the default string "Blue." 

  5.    Enum provides a valueOf method, and this method should be relatively toString method is the.    Call valueOf ( "Blue") will return to Color.Blue so we rewrite toString method in their own time should be taken note of this, a relative, should be to rewrite valueOf method. 
  6.    Enum also provides values method, this method allows you to easily traverse all the enumeration values. 

  7.    Enum oridinal there is a method, this method returns an enumeration value of the enumerated categories in the order, the order in accordance with the order of enumeration values statement is set here Color.Red.ordinal () will return 0. 

  Understand these basic characteristics, we have to look at how to use them. 
  1.    Traversing all enumerated value. Know there are values, we can use ForEach Qingcheshoulu cycle to traverse the enumeration values. 
  For (Color c: Color.values ()) 
  System.out.println ( "find value:" + c); 

  2.    Enum defined in the methods and variables, for example, we can add a method for Color returns a random color. 
  (Public enum Color 
Red,
Green,
Blue;

  / * 
  * Definition of a variable value that the number of enumeration. 
  * (I am a bit strange why the sun did not give a size enum direct method). 
  * / 
  Private static int number = Color.values (). Length; 

  / ** 
  * Returns a random enumeration value 
  @ Return a random enum value. 
  * / 
  Public static Color getRandomColor () ( 
  Long random = System.currentTimeMillis ()% number; 
  Switch ((int) random) ( 
  Case 0: 
  Return Color.Red; 
  Case 1: 
  Return Color.Green; 
  Case 2: 
  Return Color.Blue; 
  Default: return Color.Red; 
  ) 
  ) 
  ) 

  This can be seen in the enumerated types, variables and methods defined in the general category and define ways and there is no difference between variables.    Only to the attention of the only definition of variables and methods must be placed on all enumerated behind the definition of value, otherwise the compiler will be given a mistake. 

  3.    Review contains (Override) toString, valueOf method. 
  We already know ahead enum provided toString, valueOf method, a lot of time, we need to review the default contained toString method, then the enum how we do.    In fact, and this review contains a general class of toString method there is no distinction. 
….
  Public String toString () ( 
  Switch (this) ( 
  Case Red: 
  Return "Color.Red"; 
  Case Green: 
  Return "Color.Green"; 
  Case Blue: 
  Return "Color.Blue"; 
  Default: 
  Return "Unknow Color"; 

  ) 
  ) 
….
  Then we can see that, at this time in front of Ergodic reuse code is printed 
Color.Red
Color.Green
Color.Blue
  Rather than 
Red
Green
Blue.
  ToString indeed can be seen lining out.    ToString generally contained in the review time at the same time we should also review contained valueOf method, in order to maintain their mutual consistency. 

  4.    Use constructor function. 
  Although we can not have a public enum the constructor function, but we still can be defined private constructor, enum internal use.    Color or use this example. 

  (Public enum Color 
  Red ( "This is Red") 
  Green ( "This is Green") 
  Blue ( "This is Blue"); 

  Private String desc; 

  Color (String desc) ( 
  This.desc = desc; 
  ) 

  Public String getDesc () ( 
  Return this.desc; 
  ) 

  ) 
  Here we provide for each of the color of a note, and then defines a constructor function that accept this information. 
  Attention should be paid to be here constructor function for the public or protected, thus ensuring constructor function for internal use only, a new client code can not be enumerated value of the examples out.    This is fully consistent with reasonable, because we know that enumerated public static final value is the only constant. 

  5.    Achieving specific interface we already know enum can define variables and methods, it is to achieve a common interface and an interface to achieve the same class, for example by not here. 

  6.    Enumerating the definition of his or her own way. 
  We can see in front of the enum definition some ways, in fact, we can even enumerated value for each method.    In this way, our previous review contained toString example can be rewritten as such. 
  (Public enum Color 
  Red ( 
  Public String toString () ( 
  Return "Color.Red"; 
  ) 
  ) 
  Green ( 
  Public String toString () ( 
  Return "Color.Green"; 
  ) 
  ) 
  Blue ( 
  Public String toString () ( 
  Return "Color.Blue"; 
  ) 

  ); 
  ) 
  Logically speaking, this was more than a "global" toString method to clear some. 

  Overall, enum, as a new definition of the type, is to help programmers to write code more simple and more accessible, individuals generally do not need too many feel that the use of enum some advanced features, and easy-to-understand otherwise contrary to the original intention. 

  </ Td> </ tr> <tr> 

  ↑ Back 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags: ,

Releated Java Articles

Comments

Leave a Reply