int compareTo(E o)
比较此枚举与指定对象的顺序。
String name()
返回此枚举常量的名称,在其枚举声明中对其进行声明。
int ordinal()
返回枚举常量的序数(它在枚举声明中的位置,其中初始常量序数为零)。
String toString()
返回枚举常量的名称,它包含在声明中。
public class Test { public static void main(String[] args) throws Exception { Direction d1 = Direction.FRONT; Direction d2 = Direction.BEHIND; Direction d3 = Direction.LEFT; Direction d4 = Direction.RIGHT; System.out.println(d1.compareTo(d1));// 0 System.out.println(d1.compareTo(d4));// -3 System.out.println(d4.compareTo(d1));// 3 System.out.println("----"); System.out.println(d1.name());// FRONT System.out.println(d2.name());// BEHIND System.out.println(d3.name());// LEFT System.out.println(d4.name());// RIGHT System.out.println("----"); System.out.println(d1.ordinal());// 0 System.out.println(d2.ordinal());// 1 System.out.println(d3.ordinal());// 2 System.out.println(d4.ordinal());// 3 System.out.println("----"); System.out.println(d1.toString());// FRONT System.out.println(d2.toString());// BEHIND System.out.println(d3.toString());// LEFT System.out.println(d4.toString());// RIGHT } } enum Direction { FRONT("前"), BEHIND("后"), LEFT("左"), RIGHT("右"); String name; private Direction(String name) { this.name = name; } }