Rhythmk 一步一步学 JAVA (18): Enum枚举学习

时间:2022-11-20 22:12:08

枚举定义:

public enum SizeEnum {
SMALL,
BIG,
BIGEST
};
public enum SizeStringEnum {

    SMALL("小"),
BIG("大"),
BIGEST("巨大"); private String strSizeString; public String toString() {
return strSizeString;
}
private SizeStringEnum(String str) {
this.strSizeString = str; } }

常用方法:

1、values():

获取当前枚举的所有对象属性:

如:

//1、
SizeStringEnum sizeStringEnum=SizeStringEnum.BIG;
for(SizeStringEnum size: sizeStringEnum.values())
{
System.out.println(size);
} //2、
for(SizeStringEnum size: SizeStringEnum.values())
{
System.out.println(size);
}

2、Enum.valueOf

通过枚举值寻找枚举对象

        sizeStringEnum=Enum.valueOf(SizeStringEnum.class, "BIG");