• 非零基础自学Java (老师:韩顺平) 第11章 枚举和注解 【1 枚举】


    非零基础自学Java (老师:韩顺平)

    ✈【【零基础 快速学Java】韩顺平 零基础30天学会Java】

    第11章 枚举和注解

    11.1 看一个需求

    创建季节(Season)对象

    举个栗子

    package com.dingjiaxiong.enum_;
    
    /**
     * ClassName: Enumeration01
     * date: 2022/9/4 13:17
     *
     * @author DingJiaxiong
     */
    
    public class Enumeration01 {
        public static void main(String[] args) {
            Season spring = new Season("春天","温暖");
            Season winter = new Season("冬天","寒冷");
            Season summer = new Season("夏天","炎热");
            Season autumn = new Season("秋天","凉爽");
    
    //        Season other = new Season("红天","~~~~~~~~");
        }
    }
    
    
    class Season{ //类
        private String name;
        private String desc;
    
        public Season(String name, String desc) {
            this.name = name;
            this.desc = desc;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getDesc() {
            return desc;
        }
    
        public void setDesc(String desc) {
            this.desc = desc;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    11.2 分析问题
    11.2.1 创建Season对象的特点
    • 季节的值是有限的几个值(春夏秋冬)
    • 只读,不需要修改
    11.3 解决方案 - 枚举
    • 枚举的英文 —— enumeration,简写enum
    • 枚举是一组常量的集合
    • 【枚举属性一种特殊的类,里面只包含一组有限的特定的对象】
    11.4 枚举的两种实现方式
    • 自定义类实现枚举
    • 使用enum关键字实现枚举
    11.5 自定义类实现枚举 - 应用案例
    • 不需要提供setXxx方法,因为枚举对象值通常为只读.
    • 对枚举对象/属性使用final + static共同修饰,实现底层优化.
    • 枚举对象名通常使用全部大写,常量的命名规范.
    • 枚举对象根据需要,也可以有多个属性

    【举个栗子】

    package com.dingjiaxiong.enum_;
    
    /**
     * ClassName: Enumeration02
     * date: 2022/9/4 13:24
     *
     * @author DingJiaxiong
     */
    
    public class Enumeration02 {
        public static void main(String[] args) {
            System.out.println(Season.AUTUMN);
            System.out.println(Season.SPRING);
        }
    }
    
    class Season{ //类
        private String name;
        private String desc;
    
        //定义四个固定对象
        public static final Season SPRING = new Season("春天","温暖");
        public static final Season WINTER = new Season("冬天","寒冷");
        public static final Season AUTUMN = new Season("秋天","凉爽");
        public static final Season SUMMER = new Season("夏天","炎热");
    
        private Season(String name, String desc){
            this.name = name;
            this.desc = desc;
        }
    
        public String getName() {
            return name;
        }
    
        public String getDesc() {
            return desc;
        }
    
        @Override
        public String toString() {
            return "Season{" +
                    "name='" + name + '\'' +
                    ", desc='" + desc + '\'' +
                    '}';
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    运行结果

    在这里插入图片描述

    11.6 自定义类实现枚举 - 小结
    11.6.1 使用自定义类实现枚举 ,特点
    • 构造器私有化
    • 本类创建一组对象
    • 对外暴露对象(public static final修饰符)
    • 可以提供get方法,但是不提供set
    11.7 enum关键字实现枚举 - 快速入门
    11.7.1 说明

    【举个栗子】使用enum实现前面的案例

    package com.dingjiaxiong.enum_;
    
    /**
     * ClassName: Enumeration03
     * date: 2022/9/4 13:30
     *
     * @author DingJiaxiong
     */
    
    public class Enumeration03 {
        public static void main(String[] args) {
            System.out.println(Season.SPRING);
            System.out.println(Season.WINTER);
        }
    }
    
    enum Season{ //类
        SPRING("春天","温暖"),WINTER("冬天","寒冷"),AUTUMN("秋天","凉爽"),SUMMER("夏天","炎热");
    
        private String name;
        private String desc;
    
        private Season(){}
    
        private Season(String name, String desc) {
            this.name = name;
            this.desc = desc;
        }
    
        public String getName() {
            return name;
        }
    
        public String getDesc() {
            return desc;
        }
    
        @Override
        public String toString() {
            return "Season{" +
                    "name='" + name + '\'' +
                    ", desc='" + desc + '\'' +
                    '}';
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    运行结果

    在这里插入图片描述

    11.7.2 enum关键字实现枚举注意事项
    • 使用 enum 关键字开发一个枚举类时,默认会继承 Enum 类, 而且是一个 final 类
    • 传统的 public static final Season2 SPRING = new Season2(“春天”, “温暖”); 简化成 SPRING(“春天”, “温暖”), 这里必须知道,它调用的是哪个构造器.
    • 如果使用无参构造器 创建 枚举对象,则实参列表和小括号都可以省略
    • 当有多个枚举对象时,使用,间隔,最后有一个分号结尾
    • 枚举对象必须放在枚举类的行首.
    11.9 enum常用方法说明

    说明:使用关键字 enum 时,会隐式继承 Enum 类, 这样我们就可以使用 Enum 类相关的方法。

    在这里插入图片描述

    11.10 enum常用方法应用实例
    • toString:Enum 类已经重写过了,返回的是当前对象名,子类可以重写该方法,用于返回对象的属性信息
    • name:返回当前对象名(常量名),子类中不能重写
    • ordinal:返回当前对象的位置号,默认从 0 开始
    • values:返回当前枚举类中所有的常量
    • valueOf:将字符串转换成枚举对象,【要求字符串必须为已有的常量名,否则报异常】
    • compareTo:比较两个枚举常量,比较的就是编号

    【举个栗子】

    package com.dingjiaxiong.enum_;
    
    import java.security.PublicKey;
    
    /**
     * ClassName: EnumMethod
     * date: 2022/9/4 13:39
     *
     * @author DingJiaxiong
     */
    
    public class EnumMethod {
        public static void main(String[] args) {
            Season autumn = Season.AUTUMN;
    
            //输出枚举对象的名字
            System.out.println(autumn.name());
            //输出编码,从0开始
            System.out.println(autumn.ordinal()); //3
            //含有定义的所有枚举对象
            Season[] values = Season.values();
            System.out.println("===遍历取出枚举对象(增强for)===");
            for(Season season : values){
                System.out.println(season);
            }
    
            //valueOf:将字符串转换成枚举对象【要求字符串必须为已有的常量名,否则报异常】
            Season autumn1 = Season.valueOf("AUTUMN");
            System.out.println("autumn1 = " + autumn1);
            System.out.println(autumn == autumn1);
    
            //compareTo:比较两个枚举常量,比较的就是编号
            System.out.println(Season.AUTUMN.compareTo(Season.SUMMER)); // 3 - 4
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    运行结果

    在这里插入图片描述

    增强for

    package com.dingjiaxiong.enum_;
    
    /**
     * ClassName: ForTest
     * date: 2022/9/4 13:46
     *
     * @author DingJiaxiong
     */
    
    public class ForTest {
        public static void main(String[] args) {
            int[] nums = {1 ,2 ,9};
            //普通的for循环
            System.out.println("====普通的for循环====");
            for (int i = 0; i < nums.length; i++) {
                System.out.println(nums[i]);
            }
    
            System.out.println("====增强的for====");
            //依次从nums数组中取出数据赋给i,如果取出完毕,退出for
            for (int i : nums){
                System.out.println("i = " + i);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    运行结果

    在这里插入图片描述

    11.11 enum实现接口
    • 使用 enum 关键字后,就不能再继承其它类了,因为 enum 会隐式继承 Enum,而 Java 是单继承机制。

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

      enum 类名 implements 接口1,接口2{}
      
      • 1

    【举个栗子】

    package com.dingjiaxiong.enum_;
    
    /**
     * ClassName: EnumDetail
     * date: 2022/9/4 13:50
     *
     * @author DingJiaxiong
     */
    
    public class EnumDetail {
        public static void main(String[] args) {
            Music.CLASSICMUSIC.playing();
        }
    }
    
    class A{}
    
    //报错,不能继承
    //enum Season3 extends A{
    //
    //}
    
    interface IPlaying{
        public void playing();
    }
    
    enum Music implements IPlaying{
        CLASSICMUSIC;
    
        @Override
        public void playing() {
            System.out.println("播放好听的音乐...");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    运行结果

    在这里插入图片描述

  • 相关阅读:
    Android 解决CameraView叠加2个以上滤镜拍照黑屏的BUG (二)
    机器学习——支持向量机(SVM)
    Java图书借阅管理系统(含源码+论文+答辩PPT等)
    华测RTK采集的GPX数据如何带属性转出kml、shp进行后续的管理和分析
    【0116】PostgreSQL/MVCC
    RHCE第四天作业
    verilog实现I2C控制器 (小梅哥思路)----详细解析
    .net第三章-- C#语句的组成与使用
    【excel密码】如何给excel设置带有密码的只读模式
    selenium firefox FirefoxProfile不生效
  • 原文地址:https://blog.csdn.net/weixin_44226181/article/details/126913704