java 枚举 demo

Java

2017-12-11

67

0

技术:java8

运行环境:IDEA 15.2 + jdk8 + windows 10

demo功能:提供java 枚举

源代码

public enum TransportationsEnum {
    UNKNOWN(0, "未知"),
    CAR(1, "小汽车"),
    BUS(2, "巴士"),
    TRAIN(3, "地铁");
    private int val;
    private String name;

    TransportationsEnum(int val, String name) {
        this.val = val;
        this.name = name;
    }

    public static TransportationsEnum getByVal(int val) {
        for (TransportationsEnum item : values()) {
            if (item.getVal() == val) {
                return item;
            }
        }
        return TransportationsEnum.UNKNOWN;
    }

    public int getVal() {
        return val;
    }

    public String getName() {
        return name;
    }
}
 
 

欢迎添加微信,互相学习↑↑↑ -_-

发表评论

全部评论:0条

白老虎

programming is not only to solve problems, ways to think