package cn.itcast_02;
public class DirectionDemo {
public static void main(String[] args) {
Direction d = Direction.FRONT;
System.out.println(d);// FRONT
// public String toString()返回枚举常量的名称,它包含在声明中。
System.out.println("-------------------------");
System.out.println("-------------------------");
Direction2 d2 = Direction2.FRONT;
System.out.println(d2);
System.out.println(d2.getName());
System.out.println("-------------------------");
Direction3 d3 = Direction3.LEFT;
System.out.println(d3);
System.out.println(d3.getName());
d3.show();
}
}
package cn.itcast_02;
/*
* 通过JDK5提供的枚举类来做枚举类
*/
public enum Direction {
FRONT, BEHIND, LEFT, RIGHT;
}
package cn.itcast_02;
/*
* 通过JDK5提供的枚举类来做枚举类
*/
public enum Direction2 {
FRONT("前"), BEHIND("后"), LEFT("左"), RIGHT("右");
private Direction2(String name) {
this.name = name;
}
private String name;
public String getName() {
return name;
}
}
package cn.itcast_02;
/*
* 通过JDK5提供的枚举类来做枚举类
*/
public enum Direction3 {
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 Direction3(String name) {
this.name = name;
}
private String name;
public String getName() {
return name;
}
public abstract void show();
}