枚举类
/*
* 通过JDK5提供的枚举来做枚举类
*/
public enumDirection3 {
FRONT("前") {
@Override
public void show() {
System.out.println("前");
}
},
BEHIND("后") {
@Override
public void show() {
System.out.println("后");
}
},
LEFT("左") {
@Override
public void show() {
System.out.println("左");
}
},
RIGHT("右") {
@Override
public void show() {
System.out.println("右");
}
};
private String name;
private Direction3(String name){
this.name = name;
}
public String getName() {
return name;
}
public abstract void show();
}
public classDirectionDemo {
public static void main(String[] args) {
Direction3d3 = Direction3.FRONT;
System.out.println(d3);
System.out.println(d3.getName());
d3.show();
System.out.println("--------------");
Direction3dd = Direction3.FRONT;
dd= Direction3.LEFT;
switch (dd) {
case FRONT:
System.out.println("你选择了前");
break;
case BEHIND:
System.out.println("你选择了后");
break;
case LEFT:
System.out.println("你选择了左");
break;
case RIGHT:
System.out.println("你选择了右");
break;
}
}
}
运行结果:
FRONT
前
前
--------------
你选择了左