枚举
所有的枚举类都是enum的子类
通过 枚举类名.枚举项目名称,去访问指定的枚举项
每一个枚举项就是该枚举的一个对象
枚举也是一个类,也可以定义成员变量
枚举类的第一行必须是枚举项,最后一个枚举项后的分号可以省略,
但是如果枚举类有其他的成员,则不能省略
枚举类可以有构造方法,但必须是private修饰,默认也是private
枚举项的用法比较特殊
枚举类也可以有抽象方法,但是枚举项必须重写抽象方法
enum Seasonor {
Spring("春天"){
public void show(){
System.out.println(this.sea);
}
},
Summer("夏天"){
public void show(){
System.out.println(this.sea);
}
},
Autumn("秋天"){
public void show(){
System.out.println(this.sea);
}
},
Winter("冬天"){
public void show(){
System.out.println(this.sea);
}
};
private int age;
public String sea;
private String address;
private Seasonor(String sea) {
this.sea = sea;
}
public abstract void show();
}
public class EnumDemo{
public static void main(String[] args) {
System.out.println(Seasonor.Spring);
System.out.println(Seasonor.Summer);
System.out.println(Seasonor.Autumn);
System.out.println(Seasonor.Winter);
Seasonor spring = Seasonor.Spring;
System.out.println(spring);
}
}
public class EnumMethod {
public static void main(String[] args) {
String name=Season.SPRING.name();
System.out.println(name);
int i1=Season.SPRING.ordinal();
int i2=Season.SUMMER.ordinal();
int i3=Season.AUTUMN.ordinal();
int i4=Season.WINTER.ordinal();
System.out.println(i1);
System.out.println(i2);
System.out.println(i3);
System.out.println(i4);
int i = Season.SPRING.compareTo(Season.WINTER);
System.out.println(i);
String s = Season.SPRING.toString();
System.out.println(s);
Season season = Enum.valueOf(Season.class, "AUTUMN");
System.out.println(season);
System.out.println(Season.AUTUMN);
System.out.println(season==Season.AUTUMN);
Season[] values = Season.values();
for (Season value : values) {
System.out.println(value);
}
}
}
enum Season {
SPRING,SUMMER,AUTUMN,WINTER;
}