1. What is an enumeration type?
1. The essence of enumeration is the exhaustive method. List all possible situations and then call them in the listed situations.
2. Enumerations are similar to classes. They can also define attributes, constructors, and have getter and setter methods.
3. The values of enumeration type objects can be compared through == comparison, and of course, they can also be compared through equals.
4. The enumeration type is thread-safe and will only be loaded once.
5. Use enumeration types to optimize if-else.
2. Basic applications of enumeration types?
2.1. Create a basic enumeration class
public enum MyColors { RED, BLUE, WHITE,GREEN }
2.1.1. Basic calls of enumeration types
Obtain directly through enumeration
public static void main(String[] args) { System.out.println(MyColors.YELLOW); }
2.1.2, calling enumeration in switch
MyColors color = MyColors.RED; switch (color) { case RED: System.out.println("==red==");break; case BLUE: System.out.println("==yellow==");break; case GREEN: System.out.println("==green==");break; }
2.1.3. Basic enumeration types of loops
public static void main(String[] args) { for (MyColors color : MyColors.values()) { System.out.println(color.ordinal() + "<==ordinal value==>" + color); } }
2.2. Define methods in enumeration types
Methods allow you to specify specific behaviors for member properties, such as PARAM_ERROR indicating parameter errors.
2.2.1. Define methods in enumeration types
Description 1: Two parameters code and message are defined.
Note 2:You need to define set and get methods and parameterized constructors for code and message.
//Order status enumeration type public enum OrderStatus{ PARAM_ERROR(1, "Parameter error"), CART_EMPTY(2, "Shopping cart is empty"), ORDER_NOT_EXIST(3, "The order does not exist"); \t private Integer code; private String message; //Constructor private OrderStatus(Integer code, String message) { this.code = code; this.message = message; } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
2.2.1. Use enumeration types with methods
public static void main(String[] args) { System.out.println(OrderStatus.ORDER_NOT_EXIST); System.out.println(OrderStatus.ORDER_NOT_EXIST.getCode()); System.out.println(OrderStatus.ORDER_NOT_EXIST.getMessage()); }
Result output:
2.2.3. Enumeration class with loop method
public static void main(String[] args) { for (OrderStatus status : OrderStatus.values()) { System.out.print("status value=>" + status.name()); System.out.print(" status sequence=>" + status.ordinal()); System.out.print(" status code=>" + status.getCode()); System.out.println(" status message =>" + status.getMessage()); } }
2.3. Use enumeration types in interfaces
Sometimes there may be a lot of enumeration types created in a project. We can use enumeration types to place enumeration classes of the same type together for management.
2.3.1. Define enumeration types in the interface
//Order enumeration type public interface OrderEnum { //Order status enumeration class enum OrderStatus implements OrderEnum{ //Order error, order successful, order does not exist ORDER_ERROR,ORDER_SUCCESS,ORDER_NOT_EXIST; } //Order type enumeration class enum OrderType implements OrderEnum{ //Electronics, food, books ELECTRON,FOOD,BOOKS; } }
2.4, Use of EnumMap
EnumMap can be used to assign specific behaviors to the member properties of an enumeration. The function is similar to defining methods in the enumeration type.
2.4.1. Create enumeration class
public enum MyColors { RED, GREEN, BLANK, YELLOW }
2.4.2. Create test class
public static void main(String[] args) { \t\t//use EnumMap<MyColors, String> enumMap = new EnumMap<>(MyColors.class); enumMap.put(MyColors.RED,"red"); enumMap.put(MyColors.BLANK,"black"); enumMap.put(MyColors.GREEN,"green"); String result = enumMap.get(MyColors.RED); System.out.println("result==>" + result); } Output result: result==>red
2.5, Use of EnumSet
EnumSet can get those member attributes in the current enumeration
2.5.1. Create enumeration class
public enum MyColors { RED, GREEN, BLANK, YELLOW }
2.5.2. Create test class
//EnumSet import package path import java.util.EnumSet;
EnumSet enumSet=EnumSet.allOf(MyColors.class); System.out.print(enumSet + " "); } //Output results: [RED, GREEN, BLANK, YELLOW]
3. Project usage – enumeration optimization if-else usage
3.1. Use if-else usage
The disadvantage of this usage is that it is not very readable
String orderStatus="PAYED"; if(orderStatus.equals("PAYED")){ System.out.println("====Paid===="); }else if(orderStatus.equals("SHIPPED")){ System.out.println("====shipped===="); }else if(orderStatus.equals("ARRIVED")){ System.out.println("====Arrived===="); }else if(orderStatus.equals("SIGNEDFOR")){ System.out.println("====Signed===="); }else{ System.out.println("====Cancelled===="); }
3.2. Optimized use 1-Create interface
public interface OrderProcessInterface { //Return different results through order status public String orderStatus(String status); }
3.3. Optimized use 2-Create enumeration type implementation interface
//Order progress public enum OrderProcessEnum implements OrderProcessInterface{ /** * PAYED: paid, SHIPPED: shipped * ARRIVED: Arrived, SIGNEDFOR: Signed for receipt *CANCELLATION:Cancelled */ PAYED{ @Override public String orderStatus(String status) { // TODO Auto-generated method stub return "PAYED-paid"; } }, SHIPPED{ @Override public String orderStatus(String status) { // TODO Auto-generated method stub return "PAYED-signed"; } }, ARRIVED{ @Override public String orderStatus(String status) { // TODO Auto-generated method stub return "PAYED-reached"; } }, SIGNEDFOR{ @Override public String orderStatus(String status) { // TODO Auto-generated method stub return "PAYED-signed"; } }, CANCELLATION{ @Override public String orderStatus(String status) { // TODO Auto-generated method stub return "PAYED-Cancelled"; } } }
3.4. Optimized use 3-test enumeration type
public static void main(String[] args) { String orderStatus="PAYED"; String status = OrderProcessEnum.valueOf(orderStatus).orderStatus(orderStatus); System.out.println(status); } //Return value: PAYED-paid
The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java Skill TreeHomepageOverview 139356 people are learning the system