枚举类的使用

枚举类:

public enum ServiceType {

    PAY("支付业务", 0), POINT("点数充值", 1);

    private int value;
    private String name;

    private ServiceType(String name, int value) {
        this.name = name;
        this.value = value;
    }

    public static ServiceType getRelationByName(String name) {
        for (ServiceType rel : ServiceType.values()) {
            if (rel.name.equals(name)) {
                return rel;
            }
        }
        return null;
    }

    public static ServiceType getRalationByValue(Integer value) {
        if (value != null) {
            for (ServiceType rel : ServiceType.values()) {
                if (rel.value == value) {
                    return rel;
                }
            }
        }
        return null;
    }

    public Integer getValue() {
        return this.value;
    }

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



枚举中属性值:

    public static void main(String[] args) {
    	JSONArray result = new JSONArray();
    	for (ServiceType type : ServiceType.values()) {
            JSONObject obj = new JSONObject();
            obj.put("type", type.getValue());
            System.out.println(type.getValue());
            obj.put("name", type.getName());
            System.out.println(type.getName());
            System.out.println(type.name());
            result.add(obj);            
        }
		System.out.println();
	}

执行结果如下:





猜你喜欢

转载自blog.csdn.net/u010931123/article/details/81007585