The enum (enum) type is a new feature of Java 5, which is a novel type that allows represented to represent specific pieces of data and is all represented in type-safe form.
1, the use of constants
Before JDK1.5, we defined constants: public static fianl ..... Now, with enumerations, you can group related constants into an enumeration type, and enumerations provide more methods than constants.
Package publicenum Color { RED, GREEN, BLANK, YELLOW }
Use
Packagecom; Public classB { Public Static voidMain (string[] args) {System.out.println (isred (Color.blank)); //Result: falseSystem.out.println (isred (color.red));//Result: True } Static Booleanisred (color color) {if(Color.RED.equals (Color)) {return true ; } return false ; } }
Or the use of switch
Packagecom; Public classB { Public Static voidMain (string[] args) {showcolor (color.red); } Static voidshowcolor (color color) {Switch(color) { CaseBLANK:System.out.println (color); Break; CaseRED:System.out.println (color); Break; default: System.out.println (color); Break; } }}
2. Custom Functions
Packagecom; Public enumColor {RED ("Red", 1), Green ("green", 2), BLANK ("White", 3), Yello ("Yellow", 4); PrivateString name; Private intindex; PrivateColor (String name,intindex) { This. Name =name; This. index =index; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetIndex () {returnindex; } Public voidSetindex (intindex) { This. index =index; } }
Use
package com; public class B { public Static void main (string[] args) { Span style= "COLOR: #008000" >// Output the value of an enumeration System.out.println (Color.RED.getName ()); System.out.println (Color.RED.getIndex ()); // traverse all enumerations for (color color:Color.values ()) {System.out.println (color + "Name:" + color.getname () + "index:" + Color.getindex ()); } } }
Results:
Red
1
Red Name: Index:1
Green Name: Index:2
BLANK Name: White Index:3
Yello Name: Yellow index:4
Summarize:
1, the essence of the enumeration is the class, in the absence of enumeration, still can follow the Java most basic programming means to solve the need to use the enumeration of the place. Enumeration masks The type information of an enumeration value, unlike when you define a variable with public static final, you must specify a type. Enumerations are templates that are used to build constant data structures that can be extended. The use of enumerations enhances the robustness of the program, for example, when referencing a nonexistent enumeration value, the compiler will make an error. More use of enumerations also need to be developed in the study of creation, JAVA5, Java6 added a lot of new features, technology in the upgrade, for programmers to learn, if you love java. Otherwise people use the new features of the code you do not understand, it is called depressed.
2, enumeration in the Java family only a small portion of the weight, so I am in the project with the enumeration of the place is not a lot, after all, a project is a lot of people develop and maintain, with a strange thing, will give other colleagues cause reading difficulties. Therefore, constants are mostly defined with public static final.
Reprinted from Http://www.cnblogs.com/zhaoyanjun/p/5659811.html
Basic use of the Java enumeration class