zoukankan      html  css  js  c++  java
  • enum

    1. 定义在类内部的枚举类型

    public class qq {
    
        public static void main(String[] args) throws ParseException {
            System.out.println(Color.BLACK.id);
            System.out.println(Color.BLACK.name);
            System.out.println(Color.BLACK.desc);
        }
    
        public enum Color {
            RED(1, "red", "红色"), BLACK(2, "black", "黑色"), BLUE(3, "blue", "蓝色");
    
            private int id;
            private String name;
            private String desc;
    
            private Color(int id, String name, String desc) {
                this.id = id;
                this.name = name;
                this.desc = desc;
            }
        }
    
    }

    # 上面代码的输出结果为:

    2
    black
    黑色

    # 需要注意的关键地方:

    * 构造方法private

    * 定义在类内部的枚举类型可以直接通过它的属性获取值

     2. 定义在类外部的枚举类型

    public class qq {
    
        public static void main(String[] args) throws ParseException {
            System.out.println(Color.BLACK.getId());
            System.out.println(Color.BLACK.getName());
            System.out.println(Color.BLACK.getDesc());
        }
    
    }
    
    public enum Color {
    
        RED(1, "red", "红色"), BLACK(2, "black", "黑色"), BLUE(3, "blue", "蓝色");
    
        private int id;
        private String name;
        private String desc;
    
        private Color(int id, String name, String desc) {
            this.id = id;
            this.name = name;
            this.desc = desc;
        }
    
        public int getId() {
            return id;
        }
    
        public String getName() {
            return name;
        }
    
        public String getDesc() {
            return desc;
        }
    
    }

    # 需要注意的关键地方:

    * 通过get方法获取值

    3. 遍历

    public class qq {
    
        public static void main(String[] args) throws ParseException {
            for (Color color : Color.values()) {
                System.out.println(color.getId());
            }
            System.out.println(Color.existId(1));
        }
    
    }
    
    public enum Color {
    
        RED(1, "red", "红色"), BLACK(2, "black", "黑色"), BLUE(3, "blue", "蓝色");
    
        private int id;
        private String name;
        private String desc;
    
        private Color(int id, String name, String desc) {
            this.id = id;
            this.name = name;
            this.desc = desc;
        }
    
        public int getId() {
            return id;
        }
    
        public String getName() {
            return name;
        }
    
        public String getDesc() {
            return desc;
        }
    
        public static boolean existId(int id) {
            for (Color color : values()) {
                if (color.getId() == id) {
                    return true;
                }
            }
            return false;
        }
    }

    # 上面代码的输出结果为:

    1
    2
    3
    true

    # 需要注意的地方:

    enum 的内置方法 values()

  • 相关阅读:
    让超链接点击后不跳转,可以用href = "#",但是这个#就会锚点到页面最上边 点击链接后不跳转可以设置成
    js 小数取整的函数
    谷歌浏览器常用快捷键
    Vi问题
    UbuntuFAQ
    Ubuntu下配置C/C++开发环境
    win7硬盘安装ubuntu双系统——注意项
    怎样判断自己是否在平庸者之列?
    2012年软件开发者薪资调查报告
    VIM常用快捷键~网页上查找
  • 原文地址:https://www.cnblogs.com/lwmp/p/10606209.html
Copyright © 2011-2022 走看看