盒子
盒子

枚举学习

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
public enum MyEnum {
SPRING("春天"),
WINTER("冬天"),
SUMMER("夏天"),
AUTUMN("秋天");
private String info;
private MyEnum(String info){
this.info=info;
}
public String getInfo(){
return info;
}
public static void main(String[] args){
for(MyEnum myEnum:MyEnum.values()){
System.out.print(myEnum.getInfo()+" ");
}
System.out.println(" ");
for(MyEnum myEnum:MyEnum.values()){
System.out.print(myEnum.compareTo(MyEnum.AUTUMN)+" ");
System.out.print(myEnum.equals(MyEnum.AUTUMN)+" ");
System.out.print(myEnum==MyEnum.AUTUMN);
System.out.print(" ");
System.out.print("ordinal: "+myEnum.ordinal());
System.out.println("");
}
}
}

春天 冬天 夏天 秋天

-3 false false ordinal: 0

-2 false false ordinal: 1

-1 false false ordinal: 2

0 true true ordinal: 3

Enum实现了ComparableSerializable,故可以使用ComparablecompareTo方法

相等返回0,小于参数返回负数,大于参数返回正数

支持一下
扫一扫,支持Grooter
  • 微信扫一扫
  • 支付宝扫一扫