Enumeration of ENUM's tostring() valueof()name() and values() usage

Links to the original text: https://www.cnblogs.com/codething/p/9321174.html

After the appearance of enumeration classes from jdk5, some dictionary values can be defined using enumeration types.

The commonly used enumeration method is values(): traversing the constant values in the enumeration;

valueof(String name): Gets the constant values defined in the enumeration class by name; requires the string to be consistent with the enumerated constant names;

Get the name of the constant in the enumeration class using the enumeration object. name()

The toString() method is overridden in the enumeration class, and the name of the enumeration constant is returned.

In fact, toString() and value are opposite pairs of operations. valueOf obtains the enumerated constant object by name. toString() gets the name of the enumeration constant by enumerating the constant.

 

 1 package enumTest;
 2 
 3 /**
 4  * author:THINK
 5  * version: 2018/7/16.
 6  */
 7 public enum Color {
 8 
 9     RED(0,"Red"),
10     BLUE(1,"blue"),
11     GREEN(2,"green"),
12 
13     ;
14 
15 //    You can see that there is no difference between defining variables in enumeration types and defining methods and variables in general classes. The only thing to note is that variables and method definitions must be placed after all enumeration value definitions, otherwise the compiler will give an error.
16     private int code;
17     private String desc;
18 
19     Color(int code, String desc) {
20         this.code = code;
21         this.desc = desc;
22     }
23 
24     /**
25      * Define a static method and return the enumerated constant object through code
26      * @param code
27      * @return
28      */
29     public static Color getValue(int code){
30 
31         for (Color  color: values()) {
32             if(color.getCode() == code){
33                 return  color;
34             }
35         }
36         return null;
37 
38     }
39 
40 
41     public int getCode() {
42         return code;
43     }
44 
45     public void setCode(int code) {
46         this.code = code;
47     }
48 
49     public String getDesc() {
50         return desc;
51     }
52 
53     public void setDesc(String desc) {
54         this.desc = desc;
55     }
56 }

Test class

 1 package enumTest;
 2 
 3 /**
 4  * author:THINK
 5  * version: 2018/7/16.
 6  */
 7 public class EnumTest {
 8     public static void main(String[] args){
 9         /**
10          * Test the enumerated values()
11          *
12          */
13         String s = Color.getValue(0).getDesc();
14         System.out.println("The value obtained is:"+ s);
15 
16 
17 
18         /**
19          * Test the valueof the enumeration, where the value can be the name of the enumeration constant defined by itself
20          * The valueOf method converts the name of a String type into an enumerator, which is to find an enumerator whose literal value is equal to the parameter in the enumerator.
21          */
22 
23         Color color =Color.valueOf("GREEN");
24         System.out.println(color.getDesc());
25 
26         /**
27          * Test the toString() method of enumeration
28          */
29 
30         Color s2 = Color.getValue(0) ;
31         System.out.println("The value obtained is:"+ s2.toString());
32 
33     }

Posted by bigtimslim on Sat, 12 Oct 2019 10:02:28 -0700