枚举
枚举类型的概述
-
关键字: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和setpublic 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 ddSystem.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和setpublic 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());// 运行结果 - UPSystem.out.println("==========================");//ordinal - 获取里面枚举项的编号System.out.println(Direction.UP.ordinal());// 0System.out.println(Direction.DOWN.ordinal());// 1System.out.println(Direction.LEFT.ordinal());// 2System.out.println(Direction.RIGHT.ordinal());// 3System.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和setpublic 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;}}}
}