Enum入门【原】

 

package com.bobo.www.cxf.impl;  
  
public enum Traffic {  
  
    Red(1), Green(2), Yellow(3);//必须最前面  
  
    private int flag = 0;  
  
    private Traffic() {  
        System.out.println("init");  
    }  
  
    private Traffic(int flag) {  
        this.flag = flag;  
    }  
  
    public String toString() {  
        return String.valueOf(flag);  
    };  
  
} 

 

public class doit {  
    public static void main(String[] args) {  
        Traffic t1 = Traffic.Green;  
        for(Traffic t : Traffic.values()){  
            System.out.println(t);  
        }  
        System.out.println();  
    }  
}

 

 

标准参考样例


public enum IdType {
    SHUIWU("4", "税务登记证"),
    ZUZHIJIGOU("7", "组织机构代码证"),
    QIYE("8", "企业营业执照"),
    FAREN("9", "法人代码证"),
    SHIYEDANWEI("H", "事业单位法人证书"),
    SHEHUITUANTI("J", "社会团体登记证书"),
    MINBAN("K", "民办非企业登记证书"),
    FOREIGN("L", "外国(地区)企业常驻代表机构登记证"),
    GOV("M", "政府批文"),
    UNIFIEDSOCIALCREDITCODE("N", "统一社会信用代码"),
    JUMINSHENFENZHENG("0", "居民身份证"),
    HUZHAO("1", "护照"),
    JUNRENSHENFENZHENGJIAN("2", "军人身份证件"),
    SHEHUIBAOZHANGKA("6", "社会保障卡"),
    WUZHUANGJINGCHASHENFENZHENGJIAN("A", "武装警察身份证件"),
    GANGAOJUMINWANGLAITONGXINGZHENG("B", "港澳居民往来内地通行证"),
    TAIWANJUMINLAIWAGNDALUTONGXINGZHENG("C", "台湾居民来往大陆通行证"),
    HUKOUBU("E", "户口簿"),
    LINSHIJUMINSHENFENZHENG("F", "临时居民身份证"),
    JINGCHAZHENG("G", "警察(警官)证"),
    FOREIGNER("P", "外国人永久居留证"),
    OTHER("Z", "其他证件类型");

    private String code;
    private String name;

    private IdType(String code, String name) {
        this.code = code;
        this.name = name;
    }

    public String getCode() {
        return this.code;
    }

    public String getName() {
        return this.name;
    }

    public static String getNameByCode(String code) {
        String name = "";
        IdType[] var2 = values();
        int var3 = var2.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            IdType id = var2[var4];
            if (id.code.equals(code)) {
                name = id.name;
                break;
            }
        }

        return name;
    }
}

 

 

 

参考

Java基础系列:switch+enum==>https://blog.csdn.net/xin_101/article/details/108458399

posted @ 2015-05-13 10:10  苦涩泪滴  阅读(165)  评论(0编辑  收藏  举报