我学习 B站 韩顺平 老师课程的学习笔记
枚举
不用enum的实现
类似于单例设计模型
直接在类声明的内部建立几个静态的对象(访问权限除了是私有都可以),并且将构造器的访问权限设置为私有的
实际上定义的静态变量内部都是以public static final
为修饰符的
代码展示
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
| package com.enum_.Enum01;
public class EnumTest01 { public static void main(String[] args) { EnumTestClass em1 = EnumTestClass.SPRING; EnumTestClass em2 = EnumTestClass.SUMMER; EnumTestClass em3 = EnumTestClass.AUTUMN; EnumTestClass em4 = EnumTestClass.WINTER; System.out.println("em1 name: " + em1.getName() + " em1 description: " + em2.getDesc()); System.out.println("em2 name: " + em2.getName() + " em2 description: " + em2.getDesc()); System.out.println("em3 name: " + em3.getName() + " em3 description: " + em3.getDesc()); System.out.println("em4 name: " + em4.getName() + " em4 description: " + em4.getDesc()); } }
class EnumTestClass{ private String name; private String desc;
private EnumTestClass(String name, String desc) { this.name = name; this.desc = desc; }
protected static EnumTestClass SPRING = new EnumTestClass("春天","温暖"); static EnumTestClass SUMMER = new EnumTestClass("夏天","炎热"); public static EnumTestClass AUTUMN = new EnumTestClass("秋天","凉爽"); public static final EnumTestClass WINTER = new EnumTestClass("冬天","寒冷");
public String getName() { return name; }
public String getDesc() { return desc; } }
|
使用enum的实现
将class
替换为enum
基本就行了
与class的区别:
- 常量不需要再用之前那样定义了,直接
SPRING("春天","温暖");
的格式就可以替换原本的public static final EnumTestClass SPRING = new EnumTestClass("春天","温暖");
- 枚举类型的常量必须定义在enum的开头
- 多个枚举类型常量之间用逗号间隔,在最后一个枚举类型的后面加分号
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
| package com.enum_.Enum01;
public class EnumTest01 { public static void main(String[] args) { EnumTestClass em1 = EnumTestClass.SPRING; EnumTestClass em2 = EnumTestClass.SUMMER; EnumTestClass em3 = EnumTestClass.AUTUMN; EnumTestClass em4 = EnumTestClass.WINTER; System.out.println("em1 name: " + em1.getName() + " em1 description: " + em2.getDesc()); System.out.println("em2 name: " + em2.getName() + " em2 description: " + em2.getDesc()); System.out.println("em3 name: " + em3.getName() + " em3 description: " + em3.getDesc()); System.out.println("em4 name: " + em4.getName() + " em4 description: " + em4.getDesc());
EnumTestClass2 e1 = EnumTestClass2.SPRING; EnumTestClass2 e2 = EnumTestClass2.SUMMER; EnumTestClass2 e3 = EnumTestClass2.AUTUMN; EnumTestClass2 e4 = EnumTestClass2.WINTER; System.out.println("e1 name: " + e1.getName() + " e1 description: " + e1.getDesc()); System.out.println("e2 name: " + e2.getName() + " e2 description: " + e2.getDesc()); System.out.println("e3 name: " + e3.getName() + " e3 description: " + e3.getDesc()); System.out.println("e4 name: " + e4.getName() + " e4 description: " + e4.getDesc()); } }
enum EnumTestClass2{ SPRING("春天","温暖"),SUMMER("夏天","炎热"),AUTUMN("秋天","凉爽"),WINTER("冬天","寒冷"); private String name; private String desc;
EnumTestClass2(String name, String desc) { this.name = name; this.desc = desc; }
public String getName() { return name; }
public String getDesc() { return desc; } }
class EnumTestClass{ private String name; private String desc;
private EnumTestClass(String name, String desc) { this.name = name; this.desc = desc; }
protected static EnumTestClass SPRING = new EnumTestClass("春天","温暖"); static EnumTestClass SUMMER = new EnumTestClass("夏天","炎热"); public static EnumTestClass AUTUMN = new EnumTestClass("秋天","凉爽"); public static final EnumTestClass WINTER = new EnumTestClass("冬天","寒冷");
public String getName() { return name; }
public String getDesc() { return desc; } }
|
Enum类
每个枚举类都要隐式的继承Enum类
Enum类有些类方法可供使用
Enum类的方法
toString()
返回调用这个方法的枚举类对象的对象名
所以我们使用enum关键字定义的枚举类没有特殊要求其实可以不用重写toString()方法
这个方法是一个非静态方法
方法源码
1 2 3
| public String toString() { return this.name; }
|
name()
和toString()只有部分区别,toString()一般会将枚举类中的所有属性返回,但是name()只是返回枚举常量名
values()
通过枚举类直接调用此方法返回枚举类的对象数组, 也就是将枚举类的对象以一个一个数组元素合并成一个数组返回
这个方法是一个静态方法
该方法可以很方便地遍历所有枚举值
格式示例
1
| 枚举类名[] 数组名 = 枚举类名.values();
|
==注意==
不需要使用new,因为枚举对象在定义时其实已经被new了
ordinal()
找到每个枚举常量的索引,就像数组索引一样也是从0开始为第一个。
例如:
1 2
| Weeks weeks1 = Weeks.FRIDAY; System.out.println(weeks1.ordinal());
|
方法源码
1 2 3
| public final int ordinal() { return this.ordinal; }
|
valueOf()
返回指定字符串值的枚举常量。
如果没有该枚举常量则会报错 报错原因:No enum constant
相当于是返回该常量的下标
枚举类名.valueOf("枚举常量名");
1 2 3
| public static String valueOf(Object var0) { return var0 == null ? "null" : var0.toString(); }
|
==注意==通过valueOf()返回的对象和枚举常量指向的同一个对象
compareTo()
比较两个枚举常量的索引大小,也就是ordinal()返回的值的差
方法源码
1 2 3 4 5 6 7
| public final int compareTo(E var1) { if (this.getClass() != var1.getClass() && this.getDeclaringClass() != var1.getDeclaringClass()) { throw new ClassCastException(); } else { return this.ordinal - var1.ordinal; } }
|
最主要的一句就是return this.ordinal - var1.ordinal;
可以证实比较的是ordinal值,并且不只是比较大小,返回的是之间的差
eauals()
比较两个常量对象是否是同一个
方法源码
1 2 3
| public final boolean equals(Object var1) { return this == var1; }
|
Enum类方法总结
Enum类常用方法:
- toString()
- name()
- ordinal()
- values()
- valueOf()
- compareTo()
- equals()
总体测试代码
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
| package com.enum_.Enum01;
public class EnumTest02 { public static void main(String[] args) { Weeks[] weeks = Weeks.values(); for(Weeks week : weeks){ System.out.println(week); } Weeks weeks1 = Weeks.FRIDAY; System.out.println(weeks1.ordinal()); System.out.println(Weeks.valueOf("TUESDAY")); Weeks weeks2 = Weeks.WEDNESDAY; Weeks weeks3 = Weeks.valueOf("WEDNESDAY"); boolean b = weeks2 == weeks3; System.out.println(b); System.out.println(weeks2.equals(weeks3)); System.out.println(Weeks.SATURDAY.compareTo(Weeks.MONDAY)); System.out.println(weeks3.name()); } } enum Weeks { MONDAY("星期一"),TUESDAY("星期二"),WEDNESDAY("星期三"),THURSDAY("星期四"),FRIDAY("星期五"), SATURDAY("星期六"),SUNDAY("星期天");
private String name;
Weeks(String name) { this.name = name; }
@Override public String toString(){ return name; } }
|