|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
分类:
bdv002-Java面向对象
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」