public class Season {
private Season() {}//使此类不能被new
public static final Season SPRING = new Season();
public static final Season SUMMER = new Season();
public static final Season FALL = new Season();
public static final Season WINTER = new Season();
}
public class Main {
public static void main(String[] args) {
Season s = Season.SPRING;
if(s==Season.SPRING){
System.out.println("春天");
}else if (s==Season.SUMMER){
System.out.println("夏天");
}else if (s==Season.FALL){
System.out.println("秋天");
}else if(s==Season.WINTER){
System.out.println("冬天");
}
}
}
在Java中使用枚举
public class Main {
public static void main(String[] args) {
Season s = Season.WINTER;
//打印枚举的名称 WINTRE
System.out.println(s.name());
//打印枚举的索引 3
System.out.println(s.ordinal());
}
public enum Season {
SPRING,SUMMER,FALL,WINTER
}
}
原文地址:https://www.cnblogs.com/xl4ng/p/12663078.html
时间: 2024-11-12 12:23:40