|NO.Z.00083|——————————|BigDataEnd|——|Java&特殊类.V11|——|Java.v11|自定义类枚举类|在switch结构使用|

一、[枚举类的定义]——[自定义类和枚举类型在switch结构的使用]
### --- 枚举的定义

~~~     ——>        使用public static final表示的常量描述较为繁琐,
~~~     ——>        使用enum关键字来定义枚举类型取代常量,
~~~     ——>        枚举类型是从Java5开始增加的一种引用数据类型。
~~~     ——>        枚举值就是当前类的类型,也就是指向本类的对象,
~~~     ——>        默认使用public static final关键字共同修饰,
~~~     ——>        因此采用枚举类型.的方式调用。
~~~     ——>        枚举类可以自定义构造方法,
~~~     ——>        但是构造方法的修饰符必须是private,默认也是私有的。
二、编程代码
package com.yanqi.task10;

/**
 * 编程实现所有方向的枚举,所有的方向:向上、向下、向左、向右   枚举类型要求所有枚举值必须放在枚举类型的最前面
 */
public enum DirectionEnum implements DirectionInterface {
    // 2.声明本类类型的引用指向本类类型的对象
    // 匿名内部类的语法格式:接口/父类类型 引用变量名 = new 接口/父类类型() { 方法的重写 };
    // public static final Direction UP = new Direction("向上") { 方法的重写 };
    UP("向上") {
        @Override
        public void show() {
            System.out.println("贪吃蛇向上移动了一下!");
        }
    }, DOWN("向下") {
        @Override
        public void show() {
            System.out.println("贪吃蛇向下移动了一下!");
        }
    }, LEFT("向左") {
        @Override
        public void show() {
            System.out.println("左移了一下!");
        }
    }, RIGHT("向右") {
        @Override
        public void show() {
            System.out.println("右移了一下!");
        }
    };

    private final String desc; // 用于描述方向字符串的成员变量

    // 通过构造方法实现成员变量的初始化,更加灵活
    // 1.私有化构造方法,此时该构造方法只能在本类的内部使用
    private DirectionEnum(String desc) { this.desc = desc; }

    // 通过公有的get方法可以在本类的外部访问该类成员变量的数值
    public String getDesc() {
        return desc;
    }

    // 整个枚举类型只重写一次,所有对象调用同一个
    /*@Override
    public void show() {
        System.out.println("现在可以实现接口中抽象方法的重写了!");
    }*/
}
三、编程代码
package com.yanqi.task10;

public class DirectionUseTest {

    // 自定义静态方法实现根据参数指定的字符串内容来打印具体的方向信息
    public static void test1(String str) {
        switch (str) {
            case "向上":
                System.out.println("抬头望明月!"); break;
            case "向下":
                System.out.println("低头思故乡!"); break;
            case "向左":
                System.out.println("左牵黄"); break;
            case "向右":
                System.out.println("右擎苍"); break;
            default:
                System.out.println("没有这样的方向哦!");
        }
    }

    // 自定义静态方法实现根据参数指定的枚举类型来打印具体的方向信息
    public static void test2(DirectionEnum de) {
        switch (de) {
            case UP:
                System.out.println("抬头望明月!"); break;
            case DOWN:
                System.out.println("低头思故乡!"); break;
            case LEFT:
                System.out.println("左牵黄"); break;
            case RIGHT:
                System.out.println("右擎苍"); break;
            default:
                System.out.println("没有这样的方法哦!");
        }
    }

    public static void main(String[] args) {

        DirectionUseTest.test1(Direction.UP.getDesc());
        DirectionUseTest.test1("今天是个好日子!");

        System.out.println("--------------------------------------------");
        DirectionUseTest.test2(DirectionEnum.DOWN);
        //DirectionUseTest.test2("今天是个好日子!"); Error:类型不匹配,减少了出错的可能性
    }
}
四、编译打印
D:\JAVA\jdk-11.0.2\bin\java.exe "-javaagent:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=54699:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath E:\NO.Z.10000——javaproject\NO.H.00001.javase\javase\out\production\javase com.yanqi.task10.DirectionUseTest
抬头望明月!
没有这样的方向哦!
--------------------------------------------
低头思故乡!

Process finished with exit code 0

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(31)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示