标签:变量 illegal string 使用 rri 自动 thread pre cep
//自定义枚举类
class Season{
private final String seasonName;
private final String seasonDesc;
private Season(String seasonName, String seasonDesc){
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
public static final Season SPRING = new Season("春天", "春暖花开");
public static final Season SUMMER = new Season("夏天", "夏日炎炎");
public static final Season AUTUMN = new Season("秋天", "秋高气爽");
public static final Season WINTER = new Season("冬天", "冰天雪地");
//其他诉求:获取枚举类对象的属性
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
//其他诉求:提供toString方法
@Override
public String toString() {
return "Season{" +
"seasonName='" + seasonName + '\'' +
", seasonDesc='" + seasonDesc + '\'' +
'}';
}
}
//jdk5.0以后使用enum定义枚举类
enum Season1{
//1. 提供当前枚举类的对象,多个对象之间用逗号隔开,末尾分号结束,
//枚举类对象必须声明在首行
SPRING ("春天","春暖花开"),
SUMMER ("夏天", "夏日炎炎"),
AUTUMN ("秋天", "秋高气爽"),
WINTER ("冬天","冰天雪地");
private String seasonName;
private String seasonDesc;
private Season1(String seasonName, String seasonDesc){
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
}
1.私有化类的构造器,保证不能在类的外部创建其对象
2.在类的内部创建枚举类的实例。声明为:public static final
3.对象如果有实例变量,应该声明为private final,并在构造器中初始化
1.使用enum定义的枚举类默认继承了java.lang.Enum类,因此不能再继承其他类
2.枚举类的构造器只能使用private权限修饰符
3.枚举类的所有实例必须在枚举类中显式列出。列出的实例会自动添加public static final修饰
4.必须在枚举类的第一行声明枚举类对象
5.JDK1.5可以在switch表达式中使用Enum定义的枚举类的对象作为表达式,case子句可以直接使用枚举值的名字,无需添加枚举类作为限定
Season1 summer = Season1.SUMMER;
//toString():返回枚举类对象的名称
System.out.println(summer.toString());
//values():返回所有的枚举类对象构成的数组
Season1[] values = Season1.values();
for(int i = 0;i < values.length;i++){
System.out.println(values[i]);
values[i].show();
}
Thread.State[] values1 = Thread.State.values();
for (int i = 0; i < values1.length; i++) {
System.out.println(values1[i]);
}
//valueOf(String objName):返回枚举类中对象名是objName的对象。
Season1 winter = Season1.valueOf("WINTER");
//如果没有objName的枚举类对象,则抛异常:IllegalArgumentException
// Season1 winter = Season1.valueOf("WINTER1");
System.out.println(winter);
winter.show();
标签:变量 illegal string 使用 rri 自动 thread pre cep
原文地址:https://www.cnblogs.com/lilithallen/p/12541797.html