Java枚举类型代码解析

首先,枚举类型声明的对象类似于接口的,静态且常量,通过类直接调用

public enum Level {
    LEVEL1("等级一","小白"),LEVEL2("等级二","入门"),LEVEL3("等级三","熟练");
    String name;
    String desc;
    Level(String name,String desc){
        this.name = name;
        this.desc = desc;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public static void main(String[] args) {
        Level l = Level.LEVEL1;
        System.out.println(l.getName());
    }
}

下面演示的是如何获取枚举类型的对象

public static void main(String[] args) {
        Level l = Level.LEVEL1;
        System.out.println(l.getName());
        Level level = Level.valueOf("LEVEL1");
    }
}
上一篇:Global Context-Aware Progressive Aggregation Network for Salient Object Detection Notes


下一篇:基于el-cascader 封装省市区层级选择器组件