Java share notes: Custom enumeration class & use enum keyword to define enumeration class

Keywords: Java Spring JDK Programming

Before JDK 1.5, there was no enum keyword. If you want to use enumeration classes, programmers need to design them according to the rules of Java language. Starting from JDK 1.5, the Java language has added enum keyword, through which enumeration classes can be easily defined. This enumeration class has its own programming rules, and has some special methods.

The following is the author's program design for custom enumeration class and enum enumeration class respectively.

 

[1] Custom enumeration class

 1 /*-------------------------
 2 JDK1.5 Previously, through self-designed programs, from the definition of enumeration classes
 3 Let's take the season as an example to customize the enumeration class
 4 --------------------------*/
 5 package pack01;
 6 
 7 public class Season {
 8     public static void main(String[] args) {
 9 
10         FourSeasons spring = FourSeasons.SPRING;
11         FourSeasons winter = FourSeasons.WINTER;
12         
13         System.out.println( spring.getName() );
14         System.out.println( spring.toString() );
15         System.out.println();
16         System.out.println( winter.getName() );
17         System.out.println( winter.toString() );
18     }
19 }
20 
21 // Defines an enumeration class representing seasons, with 4 internal objects in total
22 class FourSeasons {
23 
24     // Defining the properties of a class: private, final, initialized in the constructor
25     private final String name;
26 
27     // Privatize the constructor so that it cannot be used outside the class
28     private FourSeasons(String name) {
29         this.name = name;
30     }
31 
32     // get Method to return the property value
33     public String getName() {
34         return name;
35     }
36 
37     // cover toString Method, set the default print value
38     public String toString() {
39         return "This is " + name;
40     }
41 
42     // Create objects inside a class
43     public static final FourSeasons SPRING = new FourSeasons("spring");
44     public static final FourSeasons SUMMER = new FourSeasons("summer");
45     public static final FourSeasons AUTUMN = new FourSeasons("autumn");
46     public static final FourSeasons WINTER = new FourSeasons("winter");
47 }

 

[2] Use enum keyword to define enumeration class

 1 /*-------------------------
 2 JDK1.5 At first, enum classes can be defined by enum keyword
 3 Using enum keyword to define enumeration class is different from the programming of custom enumeration class:
 4 ....//Replace the keyword class with the keyword enum
 5 ....//You must create internal objects at the beginning of the class body and follow certain writing rules
 6 ....//There are two common methods for enumerating classes:
 7 ........//values(): Returns all objects in an enumeration class as an array
 8 ........//valueOf(String arg0): Pass in the name of an object in the class as a string as a parameter and return the object
 9 ....//Enumeration class can implement the interface, and the abstract methods in overlay interface can be written either in the class body of enumeration class or in curly brackets after the object
10 ........//If it is written in curly braces after the object, the method is no longer public to all objects. When different objects call the same method, they can get different effects.
11 
12 Let's take season as an example to define enumeration class with enum keyword
13 --------------------------*/
14 package pack02;
15 
16 public class Season {
17     public static void main(String[] args) {
18         
19         //Test the values()Method
20         FourSeasons[] seasons = FourSeasons.values();
21         for( int i=0; i<seasons.length; ++i ) {
22             System.out.println( seasons[i].getName() );
23         }
24         System.out.println();
25         
26         //Test the valueOf()Method to create spring objects
27         FourSeasons spring = FourSeasons.valueOf("SPRING");
28         System.out.println( "valueOf(\"SPRING\"): " + spring.getName() + '\n' );
29         
30         //Create objects for summer, autumn and winter
31         FourSeasons summer = FourSeasons.SUMMER;
32         FourSeasons autumn = FourSeasons.AUTUMN;
33         FourSeasons winter = FourSeasons.WINTER;
34         spring.printWords();
35         summer.printWords();
36         autumn.printWords();
37         winter.printWords();
38         //Spring is different from the other three seasons, because SPRING Object overwrites methods in the interface
39     }
40 }
41 
42 //Define an interface to be implemented by enumeration class
43 interface Inter{
44     void printWords();
45 }
46 
47 // Defines an enumeration class representing seasons, with 4 internal objects in total
48 enum FourSeasons implements Inter { //Use enum Key and implement the above interface
49 
50     //Objects must be created at the beginning of the class body, separated by commas, and following certain writing rules
51     SPRING("spring"){
52         public void printWords() {//When creating an object, you can override the methods in the interface separately, and the overriding methods in the class body will no longer work for the object
53             System.out.println("This is spring");
54         }
55     },
56     SUMMER("summer"),
57     AUTUMN("autumn"),
58     WINTER("winter");
59     
60     // Defining the properties of a class: private, final, initialized in the constructor
61     private final String name;
62 
63     // Privatize the constructor so that it cannot be used outside the class
64     private FourSeasons(String name) {
65         this.name = name;
66     }
67     
68     // get Method returns the property value
69     public String getName() {
70         return name;
71     }
72     
73     // Overriding abstract methods in interfaces
74     public void printWords() {
75         System.out.println("There are four different seasons.");
76     }
77 }

 

Note: I hope to communicate with you and make progress together.

Posted by zaki on Fri, 31 Jan 2020 01:53:02 -0800