Java entry note 07 enumeration class

Introduction: in some cases, the objects of a class are limited and fixed, such as the season class. There are only four objects: spring, summer, autumn and winter. This kind of class with limited and fixed instances is called enumeration class. In JDK 5.0 and later, enum keyword can be used to define enumeration class, and it is inherited from java.lang.Enum class by default.

1, Use enum keyword to define enumeration class:

1. Introduction to enumeration class: the simplest enumeration class can only define enumeration values. If you need to use an instance of the enumeration class, you can call it directly through enumeration class name and enumeration value. As follows:

enum Season{
    SPRING,SUMMER,AUTUMN,WINTER;
}

Season s = Season.SPRING;
System.out.println(s.toString());//SPRING

2. Advanced enumeration class: enumeration class is a special class, so member variables, methods and constructors can also be defined.

  • Provides the objects of the current enumeration class. Multiple objects are separated by commas and ended by semicolons;
  • Declare the properties of the Season object, decorated with private final;
  • Privatize the constructor and assign values to the object properties;
  • Other demands: get the properties of enumeration class objects.

When listing enumeration values in an enumeration class, it is actually to call the constructor to create an enumeration class object, but there is no need to use the new keyword or call the constructor explicitly. There is no need to pass in parameters or even use parentheses when listing enumeration values, just because the enumeration class in front contains a constructor without parameters.

 1 enum Season{
 2     SPRING("spring","This is spring."),
 3     SUMMER("summer","This is summer."),
 4     AUTUMN("Autumn","This is autumn."),
 5     WINTER("winter","This is winter.");
 6 
 7     private final String seasonName;
 8     private final String seasonDesc;
 9 
10     private Season(String seasonName,String seasonDesc){
11         this.seasonName = seasonName;
12         this.seasonDesc = seasonDesc;
13     }
14 
15     public String getSeasonName() {
16         return seasonName;
17     }
18 
19     public String getSeasonDesc() {
20         return seasonDesc;
21     }
22 }

3. Enumeration class to implement the interface:

 1 interface Info{
 2     void show();
 3 }
 4 
 5 enum Season implements Info{
 6     SPRING("spring","This is spring."),
 7     SUMMER("summer","This is summer."),
 8     AUTUMN("Autumn","This is autumn."),
 9     WINTER("winter","This is winter.");
10 
11     private final String seasonName;
12     private final String seasonDesc;
13 
14     private Season(String seasonName,String seasonDesc){
15         this.seasonName = seasonName;
16         this.seasonDesc = seasonDesc;
17     }
18 
19     public String getSeasonName() {
20         return seasonName;
21     }
22 
23     public String getSeasonDesc() {
24         return seasonDesc;
25     }
26 
27     @Override
28     public void show() {
29         System.out.println("It's a season");
30     }
31 }
32 
33 Season s = Season.SPRING;
34 System.out.println(s.toString());
35 s.show();
36 
37 Output:
38 SPRING
39 It's a season

The abstract methods in the interface can also be implemented in enumeration values, as follows:

interface Info{
    void show();
}

enum Season implements Info{
    SPRING("spring","This is spring."){
        public void show() {
            System.out.println("This is spring.");
        }
    },
    SUMMER("summer","This is summer."){
        public void show() {
            System.out.println("This is summer.");
        }
    },
    AUTUMN("Autumn","This is autumn."){
        public void show() {
            System.out.println("This is autumn.");
        }
    },
    WINTER("winter","This is winter."){
        public void show() {
            System.out.println("This is winter.");
        }
    };

    private final String seasonName;
    private final String seasonDesc;

    private Season(String seasonName,String seasonDesc){
        this.seasonName = seasonName;
        this.seasonDesc = seasonDesc;
    }

    public String getSeasonName() {
        return seasonName;
    }

    public String getSeasonDesc() {
        return seasonDesc;
    }
}

Season s = Season.SPRING;
System.out.println(s.toString());
s.show();

//Output:
SPRING
//This is spring.

Tags: Java Spring JDK

Posted on Tue, 11 Feb 2020 03:18:19 -0800 by FireDrake