• 枚举类型


    枚举类

    枚举类是Java5的新特性,是一种特殊的类,用于表述有限且固定的对象类型。如季节类,只有四个对象;星期类,只有七个对象。其用关键字enum定义,可以有自己的成员变量、方法、构造器。一个Java源文件只能定义一个public访问权限的枚举类,且与该源文件同名。

    1. 与类的区别

    • 默认继承java.lang.Enum类,而非Object类。Enum类实现了java.lang.Serializable与java.lang.Comparable接口。
    • 非抽象枚举类默认使用final修饰,抽象枚举类默认abstract修饰
    • 枚举类构造器只能由private修饰,不添加则默认。
    • 枚举类的所有实例必须在类的首行显式写出,默认 punlic static final修饰。无需显示添加

    2. 一些方法

    • values() 静态方法,返回枚举类所有实例
    • int ordinal() 返回枚举值在枚举类中的索引值,首个索引为0
    • String toString() 返回枚举常量(实例)的名称
    • public static<T extends Enum>T valueOf(ClassenumType,String name) 静态方法
      返回返回指定枚举类中指定名称的枚举值

    【案例】

    public enum WeekEnum {
        MONDSAY,TUESDAY,WEDNESDAT,THURSDAY,FRIDAY,SATURDAY,SUNDAY;
    }
    
    public class EnumTest1 {
        public void judge(WeekEnum weekEnum){
            //switch里的表达式可以是枚举类型
            //注:当前对象是EnumTest的对象,而非枚举实例
            switch (weekEnum ){
                case SUNDAY:
                    System.out.println("today is "+weekEnum.toString()+" the index is +"+weekEnum.ordinal());
                    break;
                case MONDSAY:
                    System.out.println("today is "+weekEnum.toString()+" the index is +"+weekEnum.ordinal());
                    break;
                case TUESDAY:
                    System.out.println("today is "+weekEnum.toString()+" the index is +"+weekEnum.ordinal());
                    break;
                case WEDNESDAT:
                    System.out.println("today is "+weekEnum.toString()+" the index is +"+weekEnum.ordinal());
                    break;
                case THURSDAY:
                    System.out.println("today is "+weekEnum.toString()+" the index is +"+weekEnum.ordinal());
                    break;
                case FRIDAY:
                    System.out.println("today is "+weekEnum.toString()+" the index is +"+weekEnum.ordinal());
                    break;
                case SATURDAY:
                    System.out.println("today is "+weekEnum.toString()+" the index is +"+weekEnum.ordinal());
                    break;
            }
        }
    
        public static void main(String[] args) {
            //枚举类型有一个默认的values()方法,返回该枚举类的所有实例
            for(WeekEnum weekEnum:WeekEnum.values()){
    
                //调用了toString
                System.out.println(weekEnum+" ");
            }
            //使用枚举类型时,可通过EnumClass.variable进行访问
            new EnumTest1().judge(WeekEnum.TUESDAY);
            new EnumTest1().judge(WeekEnum.FRIDAY);
            new EnumTest1().judge(WeekEnum.WEDNESDAT);
            new EnumTest1().judge(WeekEnum.MONDSAY);
            new EnumTest1().judge(WeekEnum.SATURDAY);
            new EnumTest1().judge(WeekEnum.THURSDAY);
            new EnumTest1().judge(WeekEnum.SUNDAY);
            
            //枚举类的实例只能是枚举值,不能随意地通过new来创建枚举对象
            WeekEnum weekEnum = Enum.valueOf(WeekEnum.class, "FRIDAY");
            System.out.println(weekEnum);
    
        }
    
    
    }
    
    

    3. 成员变量与构造器

    写在首行的枚举实例,其本质是通过构造器初始化对象。默认public static final 修饰。
    如果枚举类中有final修饰的成员变量,则通常要求其在构造器中初始化,此时在列举枚举实例时就需要显示的传入参数,如:

    public enum WeekEnum {
        MONDSAY("星期一"),
        TUESDAY("星期二"),
        WEDNESDAT("星期三"),
        THURSDAY("星期四"),
        FRIDAY("星期五"),
        SATURDAY("星期六"),
        SUNDAY("星期天");
        
        private final String name;
        private WeekEnum(String name){
            this.name=name;
        }
    }
    

    相当于:
    public static final WeekEnum weekEnum=new WeekEnum("星期一")

    4. 实现接口的枚举类

    【案例】

    public interface Work {
        void work();
    }
    
    public enum WeekEnum implements Work{
        MONDSAY("星期一"),
        TUESDAY("星期二"),
        WEDNESDAT("星期三"),
        THURSDAY("星期四"),
        FRIDAY("星期五"),
        SATURDAY("星期六"),
        SUNDAY("星期天");
    
        private final String name;
        private WeekEnum(String name){
            this.name=name;
        }
        public String getName() {
            return name;
        }
        
        @Override
        public void work() {
            System.out.println("I always work in"+this.toString());
        }
    }
    
    public class EnumTest2 {
        public static void main(String[] args) {
            for(WeekEnum weekEnum:WeekEnum.values()){
                weekEnum.work();
            }
        }
    }   
    //结果:
    I always work in.MONDSAY
    I always work in.TUESDAY
    I always work in.WEDNESDAT
    I always work in.THURSDAY
    I always work in.FRIDAY
    I always work in.SATURDAY
    I always work in.SUNDAY
    

    由上面案例可知,如果由枚举类来实现接口里的方法,则每个枚举值在调用该方法时都有相同的行为方式,
    若想每个实例不同,则需分别实现抽象方法:

    public enum WeekEnum implements Work{
        MONDSAY("星期一"){
            public void work(){
                System.out.println("playing...");
            }
        },
        TUESDAY("星期二"){
            public void work(){
                System.out.println("listening..");
            }
        },
        WEDNESDAT("星期三"){
            public void work(){
                System.out.println("studying...");
            }
        },
        THURSDAY("星期四"){
            public void work(){
                System.out.println("reading...");
            }
        },
        FRIDAY("星期五"){
            public void work(){
                System.out.println("watching...");
            }
        },
        SATURDAY("星期六"){
            public void work(){
                System.out.println("eating...");
            }
        },
        SUNDAY("星期天"){
            public void work(){
                System.out.println("sleeping...");
            }
        };
    
        private final String name;
        private WeekEnum(String name){
            this.name=name;
        }
        public String getName() {
            return name;
        }
    }
    
    public class EnumTest2 {
        public static void main(String[] args) {
            for(WeekEnum weekEnum:WeekEnum.values()){
                weekEnum.work();
            }
        }
    }
    
    //结果:
    listening..
    studying...
    reading...
    watching...
    eating...
    sleeping...
    

    4. 总结

    枚举类,实际是将其可能产生的实例对象,都放在了类中。
    有点像是多类型的单例模式。

  • 相关阅读:
    vuecli 脚手架总结
    javascript 如何继承父类
    拖拽面向对象的写法
    获得焦点并且复制文本
    使用vuecli脚手架安装的eslint 容易犯错的地方
    javascript 实现分享功能
    原生javascript 获得css样式有几种方法?
    原生javascript 改写的tab选项卡
    [转载]c# 嵌入资源文件
    [转载]C# log4net应用
  • 原文地址:https://www.cnblogs.com/juzhuxiaozhu/p/12839521.html
Copyright © 2020-2023  润新知