• java 枚举


    枚举

    注意:枚举是单例的,单例要保证私有化构造器。

    1. 如何自定义枚举类
    1. 提供类的属性,声明为 private final
    2. 声明为 final 的属性,在构造器中初始化
    3. 通过公共方法(get)方法来调用属性
    4. 创建枚举类的对象:将类的对象声明 public static final

    举例如下:

    class Season {
        //1. 提供类的属性,声明为 private final
        private final String seasonName;
        private final String seasonDesc;
    
        //2. 声明为 final 的属性,在构造器中初始化
        private Season (String seasonName, String seasonDesc) {
            this.seasonName = seasonName;
            this.seasonDesc = seasonDesc;
        }
    
        //3. 通过公共的方法来调用属性
        public String getSeasonName() {
            return seasonName;
        }
        public String getSeasonDesc() {
            return seasonDesc;
        }
    
        //4. 创建枚举类的对象:将类的对象声明 public static final
        public static final Season SPRING = new Season("spring", "春暖花开");
        public static final Season SUMMER = new Season("summer", "夏日炎炎");
        public static final Season AUTUMN = new Season("autumn", "秋高气爽");
        public static final Season WINTER = new Season("winter", "白雪皑皑");
    
        @Override
        public String toString() {
            return "Season [seasonName=" + seasonName + ", seasonDesc=" + seasonDesc + "]";
        }
    
        public void show() {
            System.out.println("这是一个季节");
        }
    
    }
    
    public class TestSeason {
        public static void main(String[] args) {
            Season spring = Season.SPRING;
            System.out.println(spring);
            spring.show();
            System.out.println(spring.getSeasonName());
        }
    }
    
    2. 如何使用 enum 关键字来定义枚举类

    将对象放到最前面,并且不需要定义对象的其他属性。
    使用 enum 类来定义枚举类,可以使用下述两种方法来返回值。

    1. values() 方法:返回所有值
    2. valueOf() 方法:返回指定对象的值(要求传入的形参name是枚举类对象的名字)
      否则,报 java.lang.IllegalArgumentException 异常

    改造一下上述代码:

    enum Season {
    
        SPRING("spring", "春暖花开"),
        SUMMER("summer", "夏日炎炎"),
        AUTUMN("autumn", "秋高气爽"),
        WINTER("winter", "白雪皑皑");
    
    
        private final String seasonName;
        private final String seasonDesc;
    
        private Season (String seasonName, String seasonDesc) {
            this.seasonName = seasonName;
            this.seasonDesc = seasonDesc;
        }
    
        public String getSeasonName() {
            return seasonName;
        }
        public String getSeasonDesc() {
            return seasonDesc;
        }
    
    
    
        @Override
        public String toString() {
            return "Season [seasonName=" + seasonName + ", seasonDesc=" + seasonDesc + "]";
        }
    
        public void show() {
            System.out.println("这是一个季节");
        }
    
    }
    
    public class TestSeason {
        public static void main(String[] args) {
            Season spring = Season.SPRING;
            System.out.println(spring);
            spring.show();
            System.out.println(spring.getSeasonName());
    
            // 1. values() 方法:返回所有值
            Season[] seasons = Season.values();
            for (int i=0; i<seasons.length;i++) {
                System.out.println(seasons[i]);
            }
    
            // 2. valueOf() 方法:返回指定对象的值(要求传入的形参name是枚举类对象的名字)
            // 否则,报 java.lang.IllegalArgumentException 异常
            String str = "SPRING";
            Season sea = Season.valueOf(str);
            System.out.println(sea);
        }
    }
    
    3. 枚举类实现接口

    可以让不同的枚举类的对象调用被重写的抽象方法,执行的效果不同。(相当于让每个对象重写抽象方法)

    改造如下:

    interface Info {
        void show();
    }
    
    enum Season implements Info {
    
        SPRING("spring", "春暖花开"){
            public void show() {
                System.out.println("春天在哪里?");
            }
        },
        SUMMER("summer", "夏日炎炎"){
            public void show() {
                System.out.println("夏天在哪里?");
            }
        },
        AUTUMN("autumn", "秋高气爽"){
            public void show() {
                System.out.println("秋天在哪里?");
            }
        },
        WINTER("winter", "白雪皑皑"){
            public void show() {
                System.out.println("冬天在哪里?");
            }
        };
    
    
        private final String seasonName;
        private final String seasonDesc;
    
        private Season (String seasonName, String seasonDesc) {
            this.seasonName = seasonName;
            this.seasonDesc = seasonDesc;
        }
    
        public String getSeasonName() {
            return seasonName;
        }
        public String getSeasonDesc() {
            return seasonDesc;
        }
    
    
    
        @Override
        public String toString() {
            return "Season [seasonName=" + seasonName + ", seasonDesc=" + seasonDesc + "]";
        }
    
        public void show() {
            System.out.println("这是一个季节");
        }
    
    }
    
    public class TestSeason {
        public static void main(String[] args) {
            Season spring = Season.SPRING;
            System.out.println(spring);
            spring.show();
            System.out.println(spring.getSeasonName());
    
            // 1. values() 方法:返回所有值
            Season[] seasons = Season.values();
            for (int i=0; i<seasons.length;i++) {
                System.out.println(seasons[i]);
            }
    
            // 2. valueOf() 方法:返回指定对象的值(要求传入的形参name是枚举类对象的名字)
            // 否则,报 java.lang.IllegalArgumentException 异常
            String str = "WINTER";
            Season sea = Season.valueOf(str);
            System.out.println(sea);
            sea.show();
        }
    }
    

    完。

  • 相关阅读:
    解决Android Studio Gradle DSL method not found: 'android()'
    【转】关于ListView中notifyDataSetChanged()刷新数据不更新原因
    设计模式-单例模式
    IE浏览器让DIV居中
    Java通过DOM解析XML
    git 配置文件位置;git配置文件设置
    git config配置
    dos2unix
    文件的编码问题解决
    git diff old mode 100644 new mode 100755
  • 原文地址:https://www.cnblogs.com/weixuqin/p/11333639.html
Copyright © 2020-2023  润新知