• java中的枚举类


    • 什么是枚举、枚举类?

    枚举是指将变量的值一一列出来,变量的值只限于列举出来的值的范围内。例如:一周只有7天。

    枚举类就是一个类有多个实例,但不是无限个数的实例。

    • 创建无参构造对象的枚举类

    (1)JDK1.5前:

    public class Weekday {
        public static final Weekday MON = new Weekday();
        public static final Weekday TUE = new Weekday();
        public static final Weekday WED = new Weekday();
        public static final Weekday THU = new Weekday();
        public static final Weekday FRI = new Weekday();
    
        private Weekday(){}
    }

    创建对象:

    public static void main(String[] args) {
            Weekday mon = Weekday.MON;
            Weekday tue = Weekday.TUE;
            Weekday wed = Weekday.WED;
            Weekday thu = Weekday.THU;
            Weekday fri = Weekday.FRI;
        }

    (2)JDK1.5后:

    public enum Weekday {
        MON,TUE,WED,THU,FRI;
    }

    创建对象和上面的方法一样。

    • 创建有参构造对象的枚举类

    (1)JDK1.5前:

    public class Weekday {
    
        public static final Weekday MON = new Weekday("星期一");
        public static final Weekday TUE = new Weekday("星期二");
        public static final Weekday WED = new Weekday("星期三");
        public static final Weekday THU = new Weekday("星期四");
        public static final Weekday FRI = new Weekday("星期五");
    
        private String name;
    
        private Weekday(String name){
            this.name = name;
        }
    
        public String getName() {
            return name;
        }
    }

    创建对象(和上面一样):

    public class Demo1 {
        public static void main(String[] args) {
            Weekday mon = Weekday.MON;
            Weekday tue = Weekday.TUE;
            Weekday wed = Weekday.WED;
            Weekday thu = Weekday.THU;
            Weekday fri = Weekday.FRI;
            System.out.println(mon.getName());   // 输出结果:星期一
        }
    }

    (2)JDK1.5后:

    public enum Weekday2 {
        MON("星期一"),TUE("星期二"),WED("星期三"),THU("星期四"),FRI("星期五");
    
        private String name;
    
        private Weekday2(String name) {
            this.name = name;
        }
    
        public String getName() {
            return name;
        }
    }

    创建对象和上面的方法一样。

    • 特殊情况:枚举类里面有抽象方法

    (1)jdk1.5前:

    public abstract class Weekday {
    
        public static final Weekday MON = new Weekday("星期一"){
            @Override
            public void show() {
                System.out.println("今天是星期一");               // 匿名类。父类引用指向子类对象。
            }
        };
        /*public static final Weekday TUE = new Weekday("星期二");
        public static final Weekday WED = new Weekday("星期三");
        public static final Weekday THU = new Weekday("星期四");
        public static final Weekday FRI = new Weekday("星期五");*/
    
        private String name;
    
        private Weekday(String name){
            this.name = name;
        }
    
        public String getName() {
            return name;
        }
    
        public abstract void show();
    }

    (2)jdk1.5后:

    public enum Weekday2 {
        MON("星期一"){
            @Override
            public void show() {
                System.out.println("今天是星期一");
            }
        },TUE("星期二") {
            @Override
            public void show() {
                System.out.println("今天是星期二");
            }
        },WED("星期三") {
            @Override
            public void show() {
                System.out.println("今天是星期三");
            }
        },THU("星期四") {
            @Override
            public void show() {
                System.out.println("今天是星期四");
            }
        },FRI("星期五") {
            @Override
            public void show() {
                System.out.println("今天是星期五");
            }
        };
    
        private String name;
    
        private Weekday2(String name) {
            this.name = name;
        }
    
        public String getName() {
            return name;
        }
    
        public abstract void show();
    }
  • 相关阅读:
    ejs
    appcan.slider.js探索
    js语法重点
    canvas动画
    canvas绘图
    Bootstrap 表单
    模态框
    Node.js EventEmitter(事件队列)
    Node.js 事件循环
    react native 页面跳转
  • 原文地址:https://www.cnblogs.com/chichung/p/10300222.html
Copyright © 2020-2023  润新知