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.
1234567 |
package com; public enum Color { RED, GREEN, BLANK, YELLOW } |
Use
123456789101112131415161718192021 |
package com;
public
class
B {
public
static
void
main(String[] args) {
System.
out
.println( isRed( Color.BLANK ) ) ;
//结果: false
System.
out
.println( isRed( Color.RED ) ) ;
//结果: true
}
static
boolean isRed( Color color ){
if
( Color.RED.
equals
( color )) {
return
true
;
}
return
false
;
}
}
|
Or the use of switch
12345678910111213141516171819202122232425 |
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. Custom Functions
123456789101112131415161718192021222324252627282930 |
package com;
public
enum
Color {
RED(
"红色"
, 1), GREEN(
"绿色"
, 2), BLANK(
"白色"
, 3), YELLO(
"黄色"
, 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
1234567891011121314151617 |
package com;
public
class
B {
public
static
void
main(String[] args) {
//输出某一枚举的值
System.
out
.println( Color.RED.getName() );
System.
out
.println( Color.RED.getIndex() );
//遍历所有的枚举
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.
Basic use of the Java enumeration class