zoukankan      html  css  js  c++  java
  • 每天学会一点点(枚举enum)

    枚举的特点:

    1. enum和class、interface的地位一样
    2. 使用enum定义的枚举类默认继承了java.lang.Enum,而不是继承Object类。枚举类可以实现一个或多个接口。
    3. 枚举类的所有实例都必须放在第一行展示,不需使用new 关键字,不需显式调用构造器。自动添加public static final修饰。
    4. 使用enum定义、非抽象的枚举类默认使用final修饰,不可以被继承。
    5. 枚举类的构造器只能是私有的。

    枚举一代码示例:

    public enum  enumColor {
        RED("红色", 1), GREEN("绿色", 2), BLANK("白色", 3), YELLO("黄色", 4);
        // 成员变量
        private String name;
        private int index;
        // 构造方法
        private enumColor(String name, int index) {
            this.name = name;
            this.index = index;
        }
        public static String getName(int index){
            for (enumColor c : enumColor.values()) {
                if (c.getIndex() == index) {
                    return c.name;
                }
            }
            return null;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getIndex() {
            return index;
        }
    
        public void setIndex(int index) {
            this.index = index;
        }
    }

    测试结果:

    public class Test {
        public static void main(String[] args) {
           String name =  enumColor.getName(1);
            System.out.println(name);//红色
        }
    }

    枚举二代码示例:

    public enum enumSSS {
    
        /**
         * 开始
         */
        BEGIN("开始","开始1",1),
    
        /**
         * 进行
         */
        MONEY_ING("进行","进行2",2),
        /**
         * 结束
         */
        MONEY_END("结束","结束3",3);
    
        private String name;
        private String title;
        private int code;
        //title和name与"开始","开始1"一一对应;如果title和name互换,则getName和getTitle获得的值互换。
        enumSSS(String title, String name, int code) {
            this.title = title;
            this.name = name;
            this.code = code;
        }
    
        public static String getName(int code) {
            for (enumSSS status : enumSSS.values()) {
                if (status.getCode() == code) {
                    return status.name;
                }
            }
            return null;
        }
        public static String getTitle(int code) {
            for (enumSSS status : enumSSS.values()) {
                if (status.getCode() == code) {
                    return status.title;
                }
            }
            return null;
        }
    
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getCode() {
            return code;
        }
    
        public void setCode(int code) {
            this.code = code;
        }
    
        public String getTitle() {
            return title;
        }
    
        public void setTitle(String title) {
            this.title = title;
        }
    }

    测试结果:

    public class Test {
        public static void main(String[] args) {
           String name =  enumSSS.getName(1);
           System.out.println(name);//开始1
            String title =  enumSSS.getTitle(1);
            System.out.println(title);//开始
        }
    }
  • 相关阅读:
    java 笔记(2) 接口作为引用数据类型
    linux 笔记(5)让vi或vim显示行数和不显示行数
    linux 笔记(4)Ubuntu 使用时vi编辑器时,不能使用backspace键来进行退格或者不能正常使用
    linux 笔记(3)sudo passwd 设置root用户的密码
    matlab笔记(1) 元胞结构cell2mat和num2cell
    linux 笔记(2) 目录直接强行删除rm -rf *(删除当前目录所有的内容)
    linux 笔记(1) ctrl+c,ctrl+z,ctrl+d
    C51单片机项目:红绿灯
    C51单片机项目:时钟
    java 笔记(1)在子类中,一定要访问父类的有参构造方法?
  • 原文地址:https://www.cnblogs.com/heqiyoujing/p/10392323.html
Copyright © 2011-2022 走看看