Random Access to enum programming instances

Source: Internet
Author: User

Java enumeration (enum) Learning <br/> from: www.java1995.org Author: Claw doll <br/> views (138) Comment (0) Comment <br/> Abstract: enumeration in Java is introduced only in JDK1.5. Using enum as the keyword is a new type that allows constants to represent data fragments. All enumeration types are java. lang. subclass of The Enum class. constants in the enumeration are separated by commas (,). If a statement is followed, ';' is used after the last constant to enumerate... <br/> enumeration in Java is introduced only in JDK1.5. Using enum as the keyword is a new type that allows constants to represent data fragments. All enumeration types are java. lang. the subclass of The Enum class. constants in the enumeration are separated by commas (,). If a statement is followed, ';' is used for the last constant. All constants in the enumeration are public static final by default, this is why constants in enumeration are recommended to be fully capitalized. Although the default value is public static final, you cannot explicitly use public static final when declaring constants, otherwise, the compiler reports an error. An enumeration constant is actually a lattice instance, and its initialization relies on java. lang. for the construction method of the Enum class, see </p> <p> Java code <br/> 1. // java. lang. constructor defined in Enum, <br/> 2. // The two parameters are the names of the defined enumerated constants and their order. <Br/> 3. protected Enum (String name, int ordinal) {<br/> 4. <br/> 5. this. name = name; <br/> 6. <br/> 7. this. ordinal = ordinal; <br/> 8. <br/> 9 .} </p> <p> the default value of the first enumerated constant is 0, and the value of the other constant is 1. <br/> The following describes the definitions of simple enumeration classes: <br/> Java code <br/> 1. public enum Colors {<br/> 2. RED, BLUE, GREEN, YELLOW, GRAY; <br/> 3. // if there are no other statements after GRAY, the semicolon after it is not written. If there is a statement after it, you must add <br/> 4 .} </p> <p> let's look at a simple application: </p> <p> Java code <br/> 1. package com. iwtxokhtd. enums; <br/> 2. <B R/> 3. /** <br/> 4. * @ author Administrator <br/> 5. * <br/> 6. */<br/> 7. public enum Colors {<br/> 8. <br/> 9. /** <br/> 10. * define a color enumeration class <br/> 11. */<br/> 12. RED, <br/> 13. GREEN, <br/> 14. BLUE, <br/> 15. YELLOW, <br/> 16. GRAY; <br/> 17 .} <br/> 18. /** <br/> 19. * test Enumeration type <br/> 20. */<br/> 21. package com. iwtxokhtd. enums; <br/> 22. <br/> 23. /** <br/> 24. * @ author Administrator <br/> 25. * <br/> 26. */<br/> 27. publi C class EnumTest {<br/> 28. <br/> 29. public static void printColor (Colors color) {<br/> 30. // use switch. After jdk1.5, The enum parameter is supported, not only int, short, char, byte <br/> 31. switch (color) {<br/> 32. case RED: <br/> 33. system. out. println ("this is Red"); <br/> 34. break; <br/> 35. case GREEN: <br/> 36. system. out. println ("This is Green"); <br/> 37. break; <br/> 38. case BLUE: <br/> 39. system. out. println ("This is Blue"); <br/> 40. break; <br/> 41. cas E YELLOW: <br/> 42. system. out. println ("this is yellow"); <br/> 43. break; <br/> 44. case GRAY: <br/> 45. system. out. println ("this is gray"); <br/> 46. break; <br/> 47. default: <br/> 48. system. out. println ("other colors"); <br/> 49 .} <br/> 50 .} <br/> 51. public static void main (String [] args) {<br/> 52. // print blue <br/> 53. printColor (Colors. BLUE); <br/> 54 .} <br/> 55 .} </p> <p> Use EnumMap <br/> EnumMap is a Map implementation customized for enumeration types. It can more efficiently complete enumeration type instances than other Map implementations. Value ing. EnumMap internally uses the ordinal () method of Enumeration type to obtain the Declaration Order of the current instance. <Br/> see Example: </p> <p> Java code <br/> 1. /** <br/> 2. * EnumMap test <br/> 3. */<br/> 4. package com. iwtxokhtd. enums; <br/> 5. <br/> 6. import java. util. enumMap; <br/> 7. <br/> 8. /** <br/> 9. * @ author Administrator <br/> 10. * <br/> 11. */<br/> 12. public class EnumMapTest {<br/> 13. <br/> 14. private EnumMap <Colors, String> colors = new EnumMap <Colors, String> (Colors. class); <br/> 15. public EnumMapTest () {<br/> 16. colors. put (Colors. RED, "RED"); <br/> 17. colors. put (Colors. BLUE, "BLUE"); <br/> 18. colors. put (Colors. GRAY, "GRAY"); <br/> 19. colors. put (Colors. GREEN, "GREEN"); <br/> 20. colors. put (Colors. YELLOW, "YELLOW"); <br/> 21 .} <br/> 22. // obtain the color <br/> 23. public String getColor (Colors color) {<br/> 24. return colors. get (color); <br/> 25 .} <br/> 26. public static void main (String [] args) {<br/> 27. system. out. println (new EnumMapTest (). get Color (Colors. BLUE); <br/> 28 .} <br/> 29. <br/> 30 .} </p> <p> enumeration is actually a class. Let's take a look at the usage of enumeration as a class: </p> <p> below we put the previous class definitions into a class, see the code: </p> <p> Java code <br/> 1. /** <br/> 2. * enumeration transformation <br/> 3. */<br/> 4. package com. iwtxokhtd. enums; <br/> 5. <br/> 6. import java. util. enumSet; <br/> 7. <br/> 8. /** <br/> 9. * @ author Administrator <br/> 10. * <br/> 11. */<br/> 12. enum AnotherColors {<br/> 13. RED, GREEN, BLUE, YELLOW, GRAY; <br/> 14. public Stri Ng printColor () {<br/> 15. switch (this) {<br/> 16. case RED: <br/> 17. return "this is Red"; <br/> 18. case GREEN: <br/> 19. return "This is Green"; <br/> 20. case BLUE: <br/> 21. return "This is Blue"; <br/> 22. case YELLOW: <br/> 23. return "this is yellow"; <br/> 24. case GRAY: <br/> 25. return "this is gray"; <br/> 26. default: <br/> 27. return "other colors"; <br/> 28 .} <br/> 29 .} <br/> 30 .} <br/> 31. public class OneClassTest {<br/> 32. <br/> 33. public stat Ic void main (String [] args) {<br/> 34. // EnumSet. allOf () refers to the Set value based on all the variables of AnotherColors. For details, see JDK documentation <br/> 35. for (AnotherColors color: EnumSet. allOf (AnotherColors. class) {<br/> 36. system. out. println ("the color information defined is:" + color. printColor (); <br/> 37. <br/> 38 .} <br/> 39 .} <br/> 40 .} </p> <p> enumeration types can also define their own constructor methods, such as: </p> <p> Java code <br/> 1. /** <br/> 2. * enumeration types allow you to define your own constructor <br/> 3. */<br/> 4. package com. iwtxokhtd. enums; <br/> 5. <br/> 6. import java. util. enumSet; <br/> 7. <br/> 8. /** <br/> 9. * @ author Administrator <br/> 10. * <br/> 11. */<br/> 12. enum ConstructorEnum {<br/> 13. RED ("red", "RED"), <br/> 14. GREEN ("green", "GREEN"), <br/> 15. BLUE ("blue", "BLUE"), <br/> 16. YELLOW ("yellow", "YELLOW"), <br/> 17. GRAY ("gray", "GRAY"); <br/> 18. <br/> 19. // The fields or attributes of the enumeration class must be defined after the enumerated constant <br/> 20. private String color; <br/> 21. private String message; <br/> 22. // The default value is priv. Ate <br/> 23. constructorEnum (String color, String message) {<br/> 24. <br/> 25. this. color = color; <br/> 26. this. message = message; <br/> 27 .} <br/> 28. public String getColor () {<br/> 29. return color; <br/> 30 .} <br/> 31. public String getMessage () {<br/> 32. return message; <br/> 33 .} <br/> 34 .} <br/> 35. public class NewEnumTest {<br/> 36. <br/> 37. public static void main (String [] args) {<br/> 38. for (Co NstructorEnum color: EnumSet. allOf (ConstructorEnum. class) {<br/> 39. system. out. println ("color Key:" + color. getColor (); <br/> 40. system. out. println ("color value:" + color. getMessage (); <br/> 41 .} <br/> 42 .} <br/> 43 .} </p> <p> precautions for using enumeration types: <br/> (1) the extends keyword cannot be used for enumeration types, but the implements keyword can be used. In this way, we can extract the common behavior of different enumeration types to the interface to regulate the behavior of enumeration types. <Br/> (2) enumeration-type custom constructor cannot overwrite the default constructor. It will be executed after the default constructor. <Br/> (3) enumeration-type custom constructor must be private, but you cannot explicitly add private. Otherwise, an error occurs. <br/> (4) in the enumerated type, the definitions of enumerated constants must be placed at the top of the list before they can be the definitions of variables and methods. </P> <p> The following describes how to customize enumeration classes. For more information, see the code: </p> <p> Java code <br/> 1. /** <br/> 2. * use a custom method in the enumeration class <br/> 3. */<br/> 4. package com. iwtxokhtd. enums; <br/> 5. <br/> 6. import java. util. enumSet; <br/> 7. <br/> 8. /** <br/> 9. * @ author Administrator <br/> 10. * <br/> 11. */<br/> 12. enum MethodEnum {<br/> 13. RED {<br/> 14. public void printColor () {<br/> 15. system. out. println ("red"); <br/> 16 .} <br/> 17 .}, <br/> 18. <br/> 19. GREEN {<br/> 20. Public void printColor () {<br/> 21. system. out. println ("green"); <br/> 22 .} <br/> 23 .}, <br/> 24. <br/> 25. BLUE {<br/> 26. public void printColor () {<br/> 27. system. out. println ("blue"); <br/> 28 .} <br/> 29 .}, <br/> 30. <br/> 31. YELLOW {<br/> 32. public void printColor () {<br/> 33. system. out. println ("yellow"); <br/> 34 .} <br/> 35 .}, <br/> 36. <br/> 37. GRAY {<br/> 38. public void printColor () {<br/> 39. system. out. p Rintln ("gray"); <br/> 40 .} <br/> 41 .}; <br/> 42. public abstract void printColor (); <br/> 43 .} <br/> 44. public class MethodEnumTest {<br/> 45. <br/> 46. // test the use of a custom method to print the constant value of the enumeration class <br/> 47. public static void main (String [] args) {<br/> 48. for (MethodEnum color: EnumSet. allOf (MethodEnum. class) {<br/> 49. color. printColor (); <br/> 50 .} <br/> 51 .} <br/> 52 .} </p> <p> next, let's take a look at how to use the relevant data to find the items in the enumeration class. For an explanation, see the following code: <br/> Java code <Br/> 1. /** <br/> 2. * search for the corresponding enumerated items using the relevant data <br/> 3. */<br/> 4. package com. iwtxokhtd. enums; <br/> 5. <br/> 6. import java. util. enumSet; <br/> 7. import java. util. hashMap; <br/> 8. import java. util. map; <br/> 9. <br/> 10. /** <br/> 11. * @ author Administrator <br/> 12. * <br/> 13. */<br/> 14. enum SearchEnum {<br/> 15. RED (0), <br/> 16. GREEN (1), <br/> 17. BLUE (2), <br/> 18. YELLOW (3), <br/> 19. GRAY (4); <br/> 20. // use static Map Associate related data with enumeration items <br/> 21. private static final Map <Integer, SearchEnum> search = new HashMap <Integer, SearchEnum> (); <br/> 22. // put some associated items in Map <br/> 23. static {<br/> 24. for (SearchEnum color: EnumSet. allOf (SearchEnum. class) {<br/> 25. search. put (color. getI (), color); <br/> 26 .} <br/> 27 .} <br/> 28. private int I; <br/> 29. searchEnum (int I) {<br/> 30. this. I = I; <br/> 31 .} <br/> 32. public int getI () {<br/> 33. r Eturn I; <br/> 34 .} <br/> 35. // search <br/> 36. public static SearchEnum get (int I) {<br/> 37. return search. get (I); <br/> 38 .} <br/> 39 .} <br/> 40. public class SearchEnumTest {<br/> 41. // test search <br/> 42. public static void main (String [] args) {<br/> 43. // search for BLUE <br/> 44. system. out. println (SearchEnum. get (2); <br/> 45 .} <br/> 46 .} </p> <p> enumeration learning is here first. Note: enumeration is easy to use in some scenarios, for example: defines time and date constants, color constants, height discrete constants, item sizes, and so on, but never misuse them. </P> <p>

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.