Seven common USES of Java enumeration summary of must see

  • 2020-05-16 06:55:53
  • OfStack

Usage 1: constants

Before JDK 1.5, we defined constants as: publicstaticfianl... . Now, with enums, you can group related constants into one enum type, and enums provide more methods than constants.

Java code


public enum Color {

RED, GREEN, BLANK, YELLOW

}

Usage 2: switch

The switch statements before JDK1.6 only support int,char,enum types. Using enums makes our code more readable.

Java code


enum Signal {

GREEN, YELLOW, RED

}

public class TrafficLight {

Signal color = Signal.RED;

public void change() {

switch (color) {

case RED:

color = Signal.GREEN;

break;

case YELLOW:

color = Signal.RED;

break;

case GREEN:

color = Signal.YELLOW;

break;

}

}

}

Usage 3: add a new method to the enumeration

If you are going to customize your own method, you must add a semicolon at the end of the enum instance sequence. Also, Java requires that an enum instance be defined first.

Java code


public enum Color {

RED(" red ", 1), GREEN(" green ", 2), BLANK(" white ", 3), YELLO(" yellow ", 4);

//  Member variables 

private String name;

private int index;

//  A constructor 

private Color(String name, int index) {

this.name = name;

this.index = index;

}

//  Common methods 

public static String getName(int index) {

for (Color c : Color.values()) {

if (c.getIndex() == index) {

return c.name;

}

}

return null;

}

// get set  methods 

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getIndex() {

return index;

}

public void setIndex(int index) {

this.index = index;

}

}

Usage 4: override the enumeration method

Here is an example of the toString() method override.

Java code


public enum Color {

RED(" red ", 1), GREEN(" green ", 2), BLANK(" white ", 3), YELLO(" yellow ", 4);

//  Member variables 

private String name;

private int index;

//  A constructor 

private Color(String name, int index) {

this.name = name;

this.index = index;

}

// Override method 

@Override

public String toString() {

return this.index+"_"+this.name;

}

}

Usage 5: implement the interface

All enumerations inherit from the java.lang.Enum class. Since Java does not support multiple inheritance, enumeration objects can no longer inherit from other classes.

Java code


public interface Behaviour {

void print();

String getInfo();

}

public enum Color implements Behaviour{

RED(" red ", 1), GREEN(" green ", 2), BLANK(" white ", 3), YELLO(" yellow ", 4);

//  Member variables 

private String name;

private int index;

//  A constructor 

private Color(String name, int index) {

this.name = name;

this.index = index;

}

// Interface methods 

@Override

public String getInfo() {

return this.name;

}

// Interface methods 

@Override

public void print() {

System.out.println(this.index+":"+this.name);

}

}

Usage 6: use interfaces to organize enums

Java code


public interface Food {

enum Coffee implements Food{

BLACK_COFFEE,DECAF_COFFEE,LATTE,CAPPUCCINO

}

enum Dessert implements Food{

FRUIT, CAKE, GELATO

}

}

Usage 7: about the use of enumerated collections

java.util.EnumSet and java.util.EnumMap are two enumerated collections. EnumSet guarantees that elements in the collection are not repeated; key in EnumMap is of type enum, while value can be of any type. The use of these two collections is not described here, but can be referred to the JDK documentation.


Related articles: