枚举的原理

来源:互联网 发布:网络摄像头ip搜索器 编辑:程序博客网 时间:2024/05/24 05:47

今天看见枚举,突然一下忘记了他的原理,于是翻翻书,有记得了,温故而知新,

复习也是很重要的,哈哈,下面看看原理

package man;

class Eunm_t{
    private static final Eunm_t RED=new Eunm_t("red");                          // 提前将自己作为定量,映入进去,
    private static final Eunm_t BLUE=new Eunm_t("blue");
    
    private String title;

    public Eunm_t(String title) {                                                                     //构造函数,为初始值,赋值准备
        super();
        this.title = title;
    }
    @Override
    public String toString() {
        // TODO Auto-generated method stub                                                //返回定值数据
        return this.title;
    }
    
    public static Eunm_t getinstance(int i){                                                //选值
        switch(i){
        case 0:return RED;
        case 1:return BLUE;
        default: return null;
        }
    }
}
enum Color{
    RED,BLUE;
}
public class Enum_Tenet {
    public static void main(String[] args) {
        Eunm_t t=Eunm_t.getinstance(1);
        System.out.println(t);
        Color c=Color.BLUE;
        System.out.println(c);
        
        
    }

}

0 0
原创粉丝点击