枚举类是一种特殊的数据类型
package code06;
public class EnumClass01 {
public static void main(String[] args) {
Season spring = Season.SPRING;//获取自定义的枚举
Season summer = Season.SUMMER;
Season autumn = Season.AUTUMN;
Season winter = Season.WINTER;
System.out.println(spring);
System.out.println(summer);
System.out.println(autumn);
System.out.println(winter);
}
}
class Season {
public String name;
public String desc;
public static Season SPRING = new Season("春天", "温暖");
public static Season SUMMER = new Season("夏天", "酷热");
public static Season AUTUMN = new Season("秋天", "凉爽");
public static Season WINTER = new Season("冬天", "寒冷");
private Season(String name, String desc) {
this.name = name;
this.desc = desc;
}
@Override
public String toString() {
return "Season{" +
"name='" + name + '\'' +
", desc='" + desc + '\'' +
'}';
}
}
Season{name='春天', desc='温暖'}
Season{name='夏天', desc='酷热'}
Season{name='秋天', desc='凉爽'}
Season{name='冬天', desc='寒冷'}
下面使用枚举类实现体验枚举类的简洁方便
package code06;
public class EnumClass01 {
public static void main(String[] args) {
Season spring = Season.SPRING;//直接.就可以使用
Season summer = Season.SUMMER;
Season autumn = Season.AUTUMN;
Season winter = Season.WINTER;
System.out.println(spring);
System.out.println(summer);
System.out.println(autumn);
System.out.println(winter);
}
}
enum Season {
SPRING("春天", "温暖"),SUMMER("夏天", "酷热"),//多个枚举需要逗号区分
AUTUMN("秋天", "凉爽"),WINTER("冬天", "寒冷");
public String name;
public String desc;
private Season(String name, String desc) {
this.name = name;
this.desc = desc;
}
@Override
public String toString() {
return "Season{" +
"name='" + name + '\'' +
", desc='" + desc + '\'' +
'}';
}
}
1.枚举类定义 通过 enum 加 名字
2.多个枚举通过逗号分割
enum其实是继承自Enum类
通过javap反编译我们可以进行分析
通过反编译我们可以看到 先是定义了四个枚举类型实例
并且该Seanson在内部使用final关键字其他类无法继承 Seanson继承自Enum类
方法名 | 详细描述 |
---|---|
value() | 传递枚举类型Class对象和枚举常量名称给静态方法valueOf,会得到与参数匹配的枚举常量 |
toString | 得到当前枚举常量的名称.你可以通过重写这个方法来使得到的结果更易读 |
equals | 在枚举类型中可以使用"==" 来比较两个枚举常量是否相等.Enum提供的这个equals()方法,也是直接使用"==" 实现.它的存在是为了在Set,List和Map中使用.注意:equals()是不可变的 |
hashCode | Enum实现了hashCode()来和equals()保持一直.它也是不可变的. |
getDeclaringClass | 得到枚举常量所属枚举类型的Class对象.可以用它来判断两个枚举常量是否属于同一个枚举类型 |
name | 得到当前枚举常量的名称建议优先使用toString() |
ordinal | 得到当前枚举常量的次序 |
compareTo | 枚举类型实现了Comparable接口,这样可以比较两个枚举常量的大小(按照声明的顺序排列) |
clone | 枚举类型不能直接被Clone.为了防止子类实现克隆方法,Enum实现了一个仅抛出CloneNotSupportedException异常的不变Clone(). |
package code06;
public class EnumClass01 {
public static void main(String[] args) {
Season spring = Season.SPRING;
Season summer = Season.SUMMER;
Season autumn = Season.AUTUMN;
Season winter = Season.WINTER;
System.out.println(spring.name());//打印出SPRING
System.out.println(summer.ordinal());//输出枚举的顺序 从0开始 打印结果为1
for (Season season:Season.values()){
//如果你自己看我的反编译文件内容会发现里面有values方法
System.out.println(season);//增强for循环 遍历枚举信息
/*打印结果
Season{name='春天', desc='温暖'}
Season{name='夏天', desc='酷热'}
Season{name='秋天', desc='凉爽'}
Season{name='冬天', desc='寒冷'}
* */
}
//也可以使用valueof获取值 前提必须有这个常量否则会报异常
// 打印结果Season{name='秋天', desc='凉爽'}
Season autumn1 = Season.valueOf("AUTUMN");
System.out.println(autumn1);
//比较两个编号 结果为 -1
//解析 autumn编号为2 - winter编号3 所以结果为-1
System.out.println(autumn.compareTo(winter));
}
}
enum Season {
SPRING("春天", "温暖"),SUMMER("夏天", "酷热"),
AUTUMN("秋天", "凉爽"),WINTER("冬天", "寒冷");
public String name;
public String desc;
private Season(String name, String desc) {
this.name = name;
this.desc = desc;
}
@Override
public String toString() {
return "Season{" +
"name='" + name + '\'' +
", desc='" + desc + '\'' +
'}';
}
}