我是靠谱客的博主 漂亮石头,这篇文章主要介绍Java Enum类(常用方法详解和演示)1.Enum类的普通用法2.Enum类实现接口,现在分享给大家,希望可以做个参考。

方便日后回顾

1.Enum类的普通用法

  1. Season 枚举类
复制代码
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
/** * 四季枚举类型 * 1、枚半是一种引用数据类型 * 2、教举类型怎么定义,语法是? * 类型修饰符 enum枚举类型名{ * 枚举值1,枚举值2 * } * 枚举编译之后也是生成 class文件 * 枚举中的每一个值可以看做是常量 * 枚举类是不能进行实例化的,即无法通过枚举类构造方法进行new 的创建 * */ public enum Season { /* 这个枚举中的括号里的顺序就是构造函数中的顺序 */ SPRING("春天",123), SUMMER("夏天",456), AUTUMN("秋天",789), WINTER("冬天",101112); private final String description; private Integer month; Season(String description, Integer month) { this.description = description; this.month = month; } public Integer month(){ return month; } public String description(){ return description; } }
  1. 测试用例
复制代码
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/** * @author <MrDai> * @version <1.0> * @since <JDK11> */ public class EnumTestDemo02 { public static void main(String[] args) { /* 这里面的传入的是个枚举类型还是有用的!这个参数[String.valueOf(Season.SUMMER)]可能是在外部传入的.这样就可以筛选了! 枚举类的方法: 1.枚举类.valueOf(枚举类中的枚举字符串)[重点] valueOf()方法返回指定字符串值的枚举常量值 比如Season.valueOf("SUMMER")等价于Season.SUMMER 但是好处是里面可以进行传参了,是动态的,这在实际项目中是有比较大的用处的. Season.SUMMER就把值给定死在了'夏天' 2.枚举类.枚举字段.name() Season.AUTUMN.name():输出枚举字段(字符串类型) 3.枚举类.枚举字段.枚举类中自定义的方法() Season.AUTUMN.description():输出枚举字段中的形参内容 4.枚举类.values() 返回枚举类中所有的枚举。 5.枚举.ordinal()方法可以找到每个枚举常量的索引,就像数组索引一样。 */ switch (Season.valueOf("SUMMER")) { case SPRING: System.out.println("春天"); break; case SUMMER: System.out.println("夏天"); break; case AUTUMN: System.out.println("秋天"); break; case WINTER: System.out.println("冬天"); break; default: System.out.println("没有季节的一天~~~"); } System.out.println(Season.AUTUMN);//AUTUMN("不是字符串类型") System.out.println(Season.valueOf("AUTUMN"));//AUTUMN("不是字符串类型") System.out.println(Season.AUTUMN.name());//AUTUMN("字符串类型") //补充:2种方式判断是否是字符串类型 //1.instanceof System.out.println(Season.AUTUMN instanceof Season);//true,说明是枚举类型 System.out.println(Season.valueOf("AUTUMN") instanceof Season);//true,说明是枚举类型 System.out.println(Season.AUTUMN.name() instanceof String);//true,说明是字符串类型 //2.反射方式 try { //这里只是判断枚举类中的AUTUMN枚举的类型 System.out.println(Season.class.getDeclaredField("AUTUMN").getType());//class Season } catch (NoSuchFieldException e) { e.printStackTrace(); } System.out.println(Season.AUTUMN.name());//AUTUMN System.out.println(Season.AUTUMN.description());//秋天 System.out.println(Season.AUTUMN.month());//789 Season winter = Season.WINTER; System.out.println(winter);//WINTER Season[] values = Season.values(); for (int i = 0; i < values.length; i++) { /* WINTER SPRING SUMMER AUTUMN */ System.out.println(values[i]); } //遍历循环 for (Season season : Season.values()) { /*结果: season: SPRING season.name():SPRING 下标: 0 season: SUMMER season.name():SUMMER 下标: 1 season: AUTUMN season.name():AUTUMN 下标: 2 season: WINTER season.name():WINTER 下标: 3 */ System.out.println("season: "+season); System.out.println("season.name():"+season.name()+" 下标: "+ season.ordinal()); } } }

2.Enum类实现接口

复制代码
1
2
3
4
枚举类可以和普通类一样实现接口,但这种实现使得每个枚举实例的接口实现方法都一样。 若要为每个枚举值提供不同实现方式,需要以下方式实现 1.四季枚举类2
复制代码
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
48
49
50
51
52
53
54
55
56
57
/** * 实现四季枚举类实现接口 * * @author <MrDai> * @version <1.0> * @since <JDK11> */ public enum SeasonTwo implements SeasonDesc { //这个枚举中的括号里的顺序就是构造函数中的顺序,里面的info方法需要手动写上去 SPRING("春天", 123) { @Override public void info() { System.out.println("春天来了!"); } }, SUMMER("夏天", 456) { @Override public void info() { System.out.println("夏天来了!"); } }, AUTUMN("秋天", 789) { @Override public void info() { System.out.println("秋天来了!"); } }, WINTER("冬天", 101112) { @Override public void info() { System.out.println("冬天来了!"); } }; private final String description; private final Integer month; SeasonTwo(String description, Integer month) { this.description = description; this.month = month; } public Integer month() { return month; } public String description() { return description; } @Override public void info() { System.out.println("啥事没有"); } }
  1. 测试用例
复制代码
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
/** * @author <MrDai> * @version <1.0> * @since <JDK11> */ public class EnumTestDemo03{ public static void main(String[] args) { /* 当每个枚举中自己没有进行重写info方法,而是使用了枚举类中重写的info方法,枚举打印出的结果: 啥事没有 啥事没有 啥事没有 啥事没有 当每个枚举中自己进行了重写info方法,使用的info方法即是调用自身的info方法,枚举打印出的结果: 春天来了! 夏天来了! 秋天来了! 冬天来了! */ SeasonTwo.SPRING.info(); SeasonTwo.SUMMER.info(); SeasonTwo.AUTUMN.info(); SeasonTwo.WINTER.info(); } }
  1. 接口
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
/** * @author <MrDai> * @version <1.0> * @since <JDK11> */ public interface SeasonDesc { //定义一个抽象方法 void info(); }

最后

以上就是漂亮石头最近收集整理的关于Java Enum类(常用方法详解和演示)1.Enum类的普通用法2.Enum类实现接口的全部内容,更多相关Java内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(98)

评论列表共有 0 条评论

立即
投稿
返回
顶部