java枚举

来源:互联网 发布:单片机modbus主机程序 编辑:程序博客网 时间:2024/06/04 18:03

特点

  • 枚举类的构造器只能是私有的。
  • enum、class、interface的地位一样。
  • 使用enum定义的枚举类默认继承了java.lang.Enum。
  • 不可被继承。

使用原则

  • 类的对象有限且固定。

实例

public enum Zoo {    Tigger("老虎"), Monkey("猴"), Dog("狗"), Pig("猪"), Null;//Null调用无参构造器,其他调用有参构造器    private String chineseName;    private Zoo(){}    private Zoo(String chineseName){        this.chineseName = chineseName;    }    public String getChineseName(){        return chineseName;    }}
    public static void main(String[] args) {        for (Zoo string : Zoo.values()) {            System.out.println(string + ":" + string.getChineseName());        }        System.out.println(Zoo.Dog+"-"+Zoo.Dog.getChineseName());        System.out.println(Zoo.Null+"-"+Zoo.Null.getChineseName());    }

点此查看参考文章

1 0
原创粉丝点击