Java中枚举是一种特殊的引用类型,是类(Class)的一种,JDK1.5中开始引入枚举类型,在Java中使用enum关键字来声明枚举类,枚举类编译后默认继承了java.lang.Enum,因此枚举类不能在继承其他类,枚举一般用来声明某一特定类型的有穷集合,如用枚举表示四季
package com.test;
public enum TestEnum {
inveted(1), // 邀请奖励
active_inveted(2), // 有效邀请奖励
register(3), // 注册奖励
order(4),// 下单奖励
;
private int code;
private TestEnum(int code) {
this.code = code;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public static TestEnum getRewardTypeByCode(int code) {
for (TestEnum value : TestEnum.values()) {
if (value.getCode() == code) {
return value;
}
}
return null;
}
}
// 运用
package com.test;
public class Test1 {
public static void main(String[] args) {
System.out.println(TestEnum.getRewardTypeByCode(1));
TestEnum rewardType = TestEnum.getRewardTypeByCode(1);
if(rewardType==TestEnum.inveted) {
System.out.println("是的");
}
}
}