• 2.枚举 enum


    基本介绍

    • 枚举对应英文(enumeration,简写 enum)

    • 枚举是一组常量的集合

    • 枚举属于一种特殊的类,里面只包含一组有限的特定的对象

    • 不需要提供 setXxxx() 方法,因为枚举对象值通常为只读

    • 对枚举对象/属性使用 static+final 共同修饰

        static+final 只有修饰基本数据类型、String类型才不会加载类,修饰对象或者方法还是会加载类

        final 修饰对象(引用)只是保证引用的指向不变,但不能保证对象本身不变

    • 枚举对象名通常使用全部大写,与常量的命名规范一样

    • 枚举对象根据需要,也可以有多个属性

    1.自定义类实现枚举

    • 将构造器私有化,目的是防止被new出对象

    • 去掉 setXxxx() 方法,防止属性被修改

    • 在Season内部,直接创建固定对象

    • 对外暴露对象(通过为对象添加 public static final 修饰符)

     1 public class Demo03 {
     2     public static void main(String[] args) {
     3         System.out.println(Season.AUTUMN);
     4         System.out.println(Season.SUMMER);
     5     }
     6 }
     7 class Season{
     8     private String name;
     9     private String desc;
    10     //定义了四个对象
    11     //加final是为了使引用不能被修改
    12     public static final Season SPRING = new Season("春天", "温暖");
    13     public static final Season WINTER = new Season("冬天", "寒冷");
    14     public static final Season SUMMER = new Season("夏天", "炎热");
    15     public static final Season AUTUMN = new Season("秋天", "凉爽");
    16     
    17     private Season(String name, String desc) {
    18         this.name = name;
    19         this.desc = desc;
    20     }
    21 22     public String getName() {
    23         return name;
    24     }
    25     public String getDesc() {
    26         return desc;
    27     }
    28     @Override
    29     public String toString() {
    30         return "Season{" +
    31                 "name='" + name + '\'' +
    32                 ", desc='" + desc + '\'' +
    33                 '}';
    34     }
    35 }
    View Code

    2.使用enum关键字实现枚举

    • 使用 enum 关键字代替 class
    • 常量对象名(实参列表)
    • public static final Season2 SPRING = new Season2("春天", "温暖"); 等价于 SPRING("春天", "温暖");
    • 如果有多个对象,需要使用 ,间隔
    • 如果使用 enum 关键字来实现枚举,要求将定义的常量对象写在最前面
     1 public class Demo04 {
     2     public static void main(String[] args) {
     3         System.out.println(Season2.SPRING);
     4         System.out.println(Season2.SUMMER);
     5     }
     6 }
     7 enum  Season2{
     8     SPRING("春天", "温暖"),WINTER("夏天", "炎热"),SUMMER("夏天", "炎热"),AUTUMN("秋天", "凉爽");
     9 10     private String name;
    11     private String desc;
    12 13     private Season2(String name, String desc) {
    14         this.name = name;
    15         this.desc = desc;
    16     }
    17     public String getName() {
    18         return name;
    19     }
    20     public String getDesc() {
    21         return desc;
    22     }
    23     @Override
    24     public String toString() {
    25         return "Season{" +
    26                 "name='" + name + '\'' +
    27                 ", desc='" + desc + '\'' +
    28                 '}';
    29     }
    30 }
    View Code

    使用enum关键字实现枚举的注意事项

    • 当我们使用enum关键字开发一个枚举类时,默认会继承Enum类;而且该枚举类是一个final类

    • 如果使用无参构造器创建枚举对象,则可以省略小括号

    • 当有多个枚举对象时,使用 ,隔开,最后以一个分号结尾

    • 枚举对象必须放在枚举类的行首

    enum的常用方法

    • 使用关键字enum时,会隐式继承Enum类,这样就可以使用Enum类的相关方法
    • oString():Enum类已经重写过了,返回的是当前对象名;子类可以重写该方法,用于返回对象的属性信息
    • name():返回当前对象名(常量名),子类中不能重写
    • ordinal():返回当前对象的位置号,默认从0开始
    • values():返回当前枚举类中所有的常量对象
    • valueOf():将字符串转换成已有的枚举对象,要求字符串必须为已有的常量名,否则报异常!
    • compareTo():比较两个枚举常量的大小(编号),返回的结果是两个枚举常量的编号相减得到的数
     1 public class Demo05 {
     2     public static void main(String[] args) {
     3         Season2 autumn = Season2.AUTUMN;
     4  
     5         System.out.println(autumn.name());
     6  
     7         System.out.println(autumn.ordinal());
     8  
     9         Season2[] values = Season2.values();
    10         for (Season2 season : values) {
    11             System.out.println(season);
    12         }
    13  
    14         Season2 autumn1 = Season2.valueOf("AUTUMN");
    15         System.out.println("season1="+autumn1);
    16         System.out.println(autumn == autumn1);
    17  
    18         System.out.println(Season2.AUTUMN.compareTo(Season2.SUMMER));
    19     }
    20 }
    21 enum  Season2{
    22     SPRING("春天", "温暖"),WINTER("冬天", "寒冷"),SUMMER("夏天", "炎热"),AUTUMN("秋天", "凉爽");
    23  
    24     private String name;
    25     private String desc;
    26  
    27     private Season2(String name, String desc) {
    28         this.name = name;
    29         this.desc = desc;
    30     }
    31     public String getName() {
    32         return name;
    33     }
    34     public String getDesc() {
    35         return desc;
    36     }
    37     @Override
    38     public String toString() {
    39         return "Season{" +
    40                 "name='" + name + '\'' +
    41                 ", desc='" + desc + '\'' +
    42                 '}';
    43     }
    44 }
    View Code

     enum的使用细节

    • 使用enum关键字创建的枚举类,就不能再继承其它类了,因为使用enum创建的枚举类会隐式的继承Enum类,而Java是单继承机制

    • 枚举类和普通类一样,可以实现接口

  • 相关阅读:
    centos7 rc.local 开机不执行
    springboot与tomcat中GZip压缩
    使用Nginx对Websocket进行反向代理
    spring-data-redis 关于订阅客户端不断创建新线程的问题
    使用apache.tika判断文件类型
    简单理解TCP通信的三次握手
    logback异步输出日志(生产者消费者模型),并非批量写入日志。
    将文本(lrc,txt)文件转换成UTF-8格式
    docker入门(三)
    Spring session(redis存储方式)监听导致创建大量redisMessageListenerContailner-X线程
  • 原文地址:https://www.cnblogs.com/midiyu/p/16792751.html
Copyright © 2020-2023  润新知