[J2SE 5.0 topic] Type security enumeration

xiaoxiao2021-03-06  20

Before the type of security enumeration introduced in J2SE 5.0, I would like to briefly introduce the background of this topic. We know, in C, we can define the enumeration type to use the alias to replace different elements in a collection, usually used to describe those categories or concepts that can be classified, and limited, such as every day of the week. , Month, Color, Poker, Solar, Five continents, four oceans, seasons, subjects, operators, and so on. They usually look like this: typedef enum {spring, summer, autumn, winter} season; essentially, these alias is processed into int constant, such as 0 represents Spring, 1 represents Summer, and so on. Because these aliases are ultimately Int, so you can conduct four operations, which causes the language unclear. In addition, we can imagine that after this enumeration, we define a typedef enum {green, holmes, russell, lee, summer} name_of_teacher; because the ENUM in C does not provide distinction between the namespace, the above two enums There is a conflict. Java did not consider the concept of introducing enumeration, perhaps out of maintaining Java language concise consideration, but the transit is moved, and there is no disappearance for the need for enumeration, so there are some common Java in Java. Enumerate design mode, such as int Enum and TypeSafe Enum, there are many open source enumerations API and non-open source internal implementation. I roughly tell the int enum mode and TypeSafe Enum mode. The so-called int enum mode is to imitate the implementation of ENUM in C, such as public class season {public static final int Spring = 0; public static final int summer = 1; public static final int aut_ = 2; public static final int winter = 3 }

This model does not have much different differences in the enumeration in C, and there is a problem with the C enumeration. And TypeSafe Enum mode is much more robust:

public class Season {private final String name; private Season (String name) {this.name = name;} public String toString () {return name;} public static final Season SPRING = new Season ( "spring"); public static final Season Summer = New Season ("Summer"); Public Static Final Season Autumn = New Season ("Autumn"); Public Static Final Season Winter = New Season ("Winter");}

The latter implementation first prevents the inheritance and explicit instantiation of this class through the private construction method, so we can only get the four Season Categories, and provide a convenient TSTRING () method to get meaningful description And because this is a full-meaning class, we can easily join your own methods and logic from defining our enumeration classes. However, it is clear that it is clear that there is still a problem: such as too many features need us to achieve, and with the increase of the number of lines, the possibility of error is also increased, and we are also difficult to guarantee our customization. These logic can meet all possible situations; there is a custom enumeration class that cannot be used for Switch statements. In the end, Java decided to hug enumeration, in J2SE 5.0, we saw this change. With an actual example (Borrowing the Java official website, too lazy thinking): public enum operation {plus {double eval (double x, double y) {return x y;}}, minus {Double Eval (double X, double x - y;}}}}, time x, double y) {return x * y;}}, divide {double eval (double x, double y) {return x / y ;}}; // do arithmetic op represented by this constant Abstract Double evAl (double x, double y);}

Then you can test the following codes: public static void main (string args []) {double x = double.parsedouble (args [0]); double y = double.parsedouble (args [1]) For (Operation Op: Operation.values ​​()) {system.out.println (x " OP " Y "=" OP.EVAL (X, Y));}} How to Using enumeration, we can easily implement some of the original troubles and works? One should pay attention to: 1-J2SE 5.0 enumeration, all items automatically think STATIC (very clear this is necessary) 2 - For each enumeration class, a public method can be called. Such as values ​​() - Returns an array 3- for all enumeration items 3- For each enumeration project, you can also call basic object methods, such as Tostring () 4-In a sense, the Enum keyword is actually Represents a new type of Class, because from essentially, it is a class, and two classes have been added to more convenient processing enumeration types in the java.util package. These two classes are ENUMSET and ENUMMAP. For more detailed information about type security, refer to here.

转载请注明原文地址:https://www.9cbs.com/read-39790.html

New Post(0)