枚举

public class TestEnum {

    public static void main(String[] args) {

        for(int i=0;i<MyEnum.values().length;i++){
            System.out.println(MyEnum.values()[i].getKey()+":"+
                    MyEnum.values()[i].getValue());
        }
        MyEnum.Enum1.setKey(4);
        System.out.println(MyEnum.Enum1.getKey()+":"+MyEnum.Enum1.getValue());
    }

    public enum MyEnum{

        //多个个枚举值,注意名字并不是构造方法名
        Enum1(1,"One"),Enum2(2,"Two"),Enum3(3,"Three");

        //枚举值所包含的属性
        private int key;
        private String value;

        //构造方法
        MyEnum(int key,String value){
            this.setKey(key);
            this.setValue(value);
        }
        public int getKey() {
            return key;
        }
        public void setKey(int key) {
            this.key = key;
        }
        public String getValue() {
            return value;
        }
        public void setValue(String value) {
            this.value = value;
        }
    }
}

 

posted @ 2020-10-16 12:35  sunseeker  阅读(67)  评论(0编辑  收藏  举报