用一个枚举的实例来验证一下就知道了,看下边的代码:
- public class test {
-
- //定义一个枚举;
- public enum WeekDay{
- one,two,three,four,five,six,seven;
- }
-
- public static void main(String[] args) {
- WeekDay weekDay=WeekDay.five;
-
- //看下边这几个实例结果分别是one,two,three,four;
- System.out.println(weekDay.valueOf("one")); //one
- System.out.println(weekDay.valueOf("two")); //two
- System.out.println(weekDay.valueOf("three")); //three
- System.out.println(weekDay.valueOf("four")); //four
- //values()方法返回的是一个weekDay类型的对象数组,可以调用他的length属性。
- System.out.println(weekDay.values().length);
- }
-
- }
复制代码 |