枚举
枚举类型的概述
-
关键字:enum
-
你可以把枚举类型理解成是一个自定义的常量的序列
枚举的语法结构
定义的枚举类型文件
package com.it.xiaosi.demo01;
/**
* @Classname : direction
* @Description : TODO 枚举
* @Author : lin_refuel@qq.com
*/
public enum direction {
// 四个常量
UP,LIFT,DOWN,RIGHT;
}
测试类 - 访问上面的枚举类型
package com.it.xiaosi.demo01;
/**
* @Classname : test
* @Description : TODO 测试类
* @Author : lin_refuel@qq.com
*/
public class test {
public static void main(String[] args) {
// 访问枚举里面的成员
System.out.println(direction.DOWN);
System.out.println(direction.UP);
System.out.println(direction.RIGHT);
System.out.println(direction.LIFT);
System.out.println("=======================");
// 枚举里面的每一个成员都相当于一个direction类型
direction direction = null;
direction = direction.UP;
System.out.println(direction);
}
}
运行结果:
完整的结构
枚举类
package com.it.xiaosi.demo02;
/**
* @Classname : direction
* @Description : TODO
* @Author : lin_refuel@qq.com
*/
public enum direction {
// 四个常量
UP("上边","方向信息"),
LIFT("左边","方向信息"),
DOWN("下边","方向信息"),
RIGHT("右边","方向信息");
// 枚举对象的无参数构造方法
direction() {
}
// 定义两个成员变量
private String ds;// 方向信息
private String dd;// 方向 - 例如左
direction(String ds, String dd) {
this.ds = ds;
this.dd = dd;
}
// 成员变量的get和set
public String getDs() {
return ds;
}
public void setDs(String ds) {
this.ds = ds;
}
public String getDd() {
return dd;
}
public void setDd(String dd) {
this.dd = dd;
}
}
测试类
package com.it.xiaosi.demo02;
/**
* @Classname : test
* @Description : TODO
* @Author : lin_refuel@qq.com
*/
public class test {
public static void main(String[] args) {
// 调用枚举里面的成员,通过枚举类名字进行访问- 类似于访问静态变量 - 通过名字进行访问
System.out.println(direction.DOWN);
System.out.println(direction.UP);
System.out.println(direction.LIFT);
System.out.println(direction.RIGHT);
System.out.println("==================");
// 访问里面私有成员变量ds dd
System.out.println(direction.RIGHT +"->"+direction.RIGHT.getDd()+":"+direction.RIGHT.getDs());
}
}
运行结果:
枚举中常用的一些方法
枚举类
package com.it.xiaosi.demo03;
/**
* @Classname : direction
* @Description : TODO
* @Author : lin_refuel@qq.com
*/
public enum Direction {
// 定义出枚举里面列表
UP("向上","w"),DOWN("向下","s"),LEFT("向左","a"),RIGHT("向右","d");
// 定义两个成员变量
private String info;// 方向信息
private String data;// 对应的键位
// 枚举的无参数构造方法
Direction() {
}
// 枚举的带参数构造方法
Direction(String info, String data) {
this.info = info;
this.data = data;
}
// 两个成员变量的get和set
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
测试类
package com.it.xiaosi.demo03;
/**
* @Classname : test
* @Description : TODO 调用枚举中常用的构造方法
* @Author : lin_refuel@qq.com
*/
public class test {
public static void main(String[] args) {
// 访问Direction里面枚举项
System.out.println(Direction.UP);// 运行结果 - UP
// name() - 获取枚举项的名字
System.out.println(Direction.UP.name());// 运行结果 - UP
System.out.println("==========================");
//ordinal - 获取里面枚举项的编号
System.out.println(Direction.UP.ordinal());// 0
System.out.println(Direction.DOWN.ordinal());// 1
System.out.println(Direction.LEFT.ordinal());// 2
System.out.println(Direction.RIGHT.ordinal());// 3
System.out.println("==========================");
// 获取枚举里面枚举项
Direction up = Direction.valueOf("UP");// 获取到里面的UP枚举
System.out.println(up); // UP
// 获取一个枚举类型数组
Direction[] values = Direction.values();
for (Direction value : values) {
System.out.println(value +"< = >" +value.ordinal()+":"+value.getInfo()+":"+value.getData());
}
}
}
枚举的应用场景
定义一个枚举类
package com.it.xiaosi.demo04;
/**
* @Classname : direction
* @Description : TODO
* @Author : lin_refuel@qq.com
*/
public enum Direction {
// 定义出枚举里面列表
UP("向上","w"),DOWN("向下","s"),LEFT("向左","a"),RIGHT("向右","d");
// 定义两个成员变量
private String info;// 方向信息
private String data;// 对应的键位
// 枚举的无参数构造方法
Direction() {
}
// 枚举的带参数构造方法
Direction(String info, String data) {
this.info = info;
this.data = data;
}
// 两个成员变量的get和set
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
测试类
package com.it.xiaosi.demo04;
import java.util.Scanner;
/**
* @Classname : DirectionTest
* @Description : TODO
* @Author : lin_refuel@qq.com
*/
public class DirectionTest {
public static void main(String[] args) {
Direction direction = null;
while (true) {
switch (new Scanner(System.in).next()) {
case "w":
case "W":
direction = Direction.UP;
break;
case "s":
case "S":
direction = Direction.DOWN;
break;
case "a":
case "A":
direction = Direction.LEFT;
break;
case "d":
case "D":
direction = Direction.RIGHT;
break;
}
switch (direction) {
case UP:
System.out.println(direction.name()
+ "<>" + direction.ordinal() +
":" + direction.getInfo()
+ "<==>" + direction.getData());
break;
case LEFT:
System.out.println(direction
+ "<>" + direction.ordinal() +
":" + direction.getInfo()
+ "<==>" + direction.getData());
break;
case DOWN:
System.out.println(direction
+ "<>" + direction.ordinal() +
":" + direction.getInfo()
+ "<==>" + direction.getData());
break;
case RIGHT:
System.out.println(direction.name()
+ "<>" + direction.ordinal() +
":" + direction.getInfo()
+ "<==>" + direction.getData());
break;
}
}
}
}