java中的枚举

枚举的作用:一些程序在运行时,他需要的数据不能是任意。而必须是一定范围内的值。jdk5雨后可以采用枚举解决。

enum关键字用于定义一个枚举类。

枚举的构造方法需要是私有的。

 

实例:


package it.cast.study;

import org.testng.annotations.Test;

public class Enumration {
    @Test
    public void test(){
        print(Grade.A);
    }
    
    public void print (Grade g){
        String value =g.getvalue();
        System.out.println(value);
    }
}
enum Grade {
    A("100-90"), B("89-80"), C("79-70"), D("69-60"), E("59-0");

    public String value;

    private Grade(String value) {
        this.value = value;
    }

    public String getvalue() {
        return this.value;
    
    }
}

posted @ 2016-12-13 13:00  贱贱的小帅哥  阅读(125)  评论(0)    收藏  举报