zoukankan      html  css  js  c++  java
  • 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();
        }
    }
    

    完。

  • 相关阅读:
    创建线程的方式三:实现Callable接口 --- JDK 5.0新增
    线程的通信
    多线程的实例练习:银行账户双储户问题
    解决线程安全问题的方式三:Lock锁 --- JDK5.0新增
    演示线程的死锁问题
    Synchronized的各场景使用方法(多窗口售票例子接上篇)
    线程的【生命周期】和【线程的同步】(下面有多窗口售票例子)
    多线程:继承方式和实现方式的联系与区别
    创建多线程的方式二:实现Runnable接口
    Java项目生成可执行jar包、exe文件以及在Windows下的安装文件
  • 原文地址:https://www.cnblogs.com/weixuqin/p/11333639.html
Copyright © 2011-2022 走看看