Basic use of Java enumeration classes, java enumeration classes
The enum type is a new feature of Java 5. It is a new type that allows constants to represent specific data fragments, and all are expressed in the form of type security.
1. Use of constants
Before JDK1.5, we define constants as publicstaticfianl ..... Now, with enumeration, You can group related constants into one Enumeration type, and enumeration provides more methods than constants.
package com;public enum Color { RED, GREEN, BLANK, YELLOW }
Use
Package com; public class B {public static void main (String [] args) {System. out. println (isRed (Color. BLANK); // result: falseSystem. out. println (isRed (Color. RED); // result: true} static boolean isRed (Color color) {if (Color. RED. equals (color) {return true;} return false ;}}
Or switch usage
package com;public class B {public static void main(String[] args) {showColor( Color.RED );}static void showColor(Color color){switch ( color ) {case BLANK:System.out.println( color );break;case RED :System.out.println( color );break;default:System.out.println( color );break;}}}
2. User-Defined Functions
Package com; public enum Color {RED ("RED", 1), GREEN ("GREEN", 2), BLANK ("white", 3), YELLO ("yellow ", 4); private String name; private int index; private Color (String name, int index) {this. name = name; this. index = index;} 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 ;}}
Use
Package com; public class B {public static void main (String [] args) {// output an enumerated value System. out. println (Color. RED. getName (); System. out. println (Color. RED. getIndex (); // traverses all enumeration values for (Color color: Color. values () {System. out. println (color + "name:" + color. getName () + "index:" + color. getIndex ());}}}
Result
Red
1
RED name: RED index: 1
GREEN name: GREEN index: 2
BLANK name: White index: 3
YELLO name: yellow index: 4
Summary:
1. enumeration is essentially a class. Before enumeration, you can still use java's most basic programming methods to solve the need for enumeration. Enumeration shields the type information of enumeration values, unlike defining variables using public static final. Enumeration is a template used to build a constant data structure. This template is scalable. The use of enumeration enhances program robustness. For example, when referencing a nonexistent enumeration value, the compiler reports an error. More methods of enumeration need to be researched and created during development. java 5 and java 6 have added many new features, and the technology is being upgraded. It is necessary for programmers to learn more, if you love java. Otherwise, it is depressing if you don't understand the code that others use new features.
2. Enumeration only accounts for a small portion of the Java family. Therefore, not many examples are used in projects. After all, many developers develop and maintain a project, using a strange thing will make reading difficult for other colleagues. Therefore, constants are mostly defined using public static final.