• 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()

  • 相关阅读:
    查询不同类别中最大的一条
    thinkphp模版调用函数方法
    mysql中explain的用法
    简易PHP多文件上传源码
    JS实现:鼠标悬停图片,图片由彩色变为灰色
    PHP分页基础教程之简单分页原理
    MYSQL建立索引需要注意以下几点!!!
    php_扑克类
    详细介绍Linux shell脚本基础学习(一)
    详细介绍Linux shell脚本基础学习(二)
  • 原文地址:https://www.cnblogs.com/lwmp/p/10606209.html
Copyright © 2020-2023  润新知