Enumerations are very common in daily life. For example, SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY representing the week is an enumeration
From this mapping to the Java language, an enumeration class representing the week can be defined:
The key word to define enumeration class is enum, enumeration class object can not come out through new, inside SUNDAY, MONDAY...these are actually equivalent to the instance of enumeration class Week. These are the only ones that are fixed, and new instances cannot be created externally. Direct class when referencing. Instance name
Week w =Week.MONDAY;
The enumeration class also has a constructor, which is private by default and can only be private. Observe this code
publicenumWeek{
SUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY;Week(){
System.out.println("hello");}}publicclassTest{
publicstaticvoidmain(String[] args){
Week w =Week.FRIDAY;}}
You will find results like this
❝hello hello hello hello hello hello hello❞
The constructor is executed 7 times, exactly corresponding to the number of enumeration items in the class. In fact, the creation of this type of enumeration item is equivalent to the object that other classes call the no-argument constructor new, that is, this enumeration class creates 7 instances, so 7 hellos are output
In addition to parameterless constructors, enumeration classes also have parameterized constructors
static Direction[] values(): returns all enumeration constants of this class;
static Direction valueOf(String name): Returns the Direction constant by the name of the enumeration constant. Note that this method has a different number of parameters from the valueOf() method in the Enum class
publicclassTest{
publicstaticvoidmain(String[] args){
for(Week w :Week.values()){
System.out.println(w);}System.out.println("星期天:"+Week.valueOf("SUNDAY"));}}
There is actually no problem with this definition, but if there is a method like this:
BigDecimalcalculatePrice(double discount){
//...}
It is necessary to pass in the product discount to calculate the price. There is no type constraint when using the above constant definition. You can pass in any value of double type, and the compiler will not issue a warning. Only if you use an enum to define this case, there will be stronger type constraints:
publicclassTest{
publicstaticvoidmain(String[] args){
Week w =Week.MONDAY;switch(w){
caseMONDAY:System.out.println("周一");break;caseTUESDAY:System.out.println("周二");break;}}}
Print
❝周一❞
Implement interface, eliminate if/else
The enumeration class we created is final modified by default and inherits the Enum class by default. Therefore, other classes cannot be inherited. But you can implement the interface
And in this way, if I want to add new animals in the future, I only need to add code to the enumeration class without changing any old code, which conforms to the principle of opening and closing!
Enumerations can also be used in an implementation of the singleton pattern