1、周六周日是休息日
public enum EnumTest {
MON(1), TUE(2), WED(3), THU(4), FRI(5), SAT(6) {
@Override
public boolean isRest() {
return true;
}
},
SUN(0) {
@Override
public boolean isRest() {
return true;
}
};
private int value;
EnumTest(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public boolean isRest() {
return false;
}
}
2、使用EnumSet创建枚举集 EnumMap集合的使用
public class Test {
public static void main(String[] args) {
// EnumSet的使用
EnumSet<EnumTest> weekSet = EnumSet.allOf(EnumTest.class);
for (EnumTest day : weekSet) {
System.out.println(day);
}
System.out.println("========================================");
// EnumMap的使用
EnumMap<EnumTest, String> weekMap = new EnumMap(EnumTest.class);
weekMap.put(EnumTest.MON, "星期一");
weekMap.put(EnumTest.TUE, "星期二");
for (Iterator<Map.Entry<EnumTest, String>> iter = weekMap.entrySet().iterator(); iter.hasNext();) {
Map.Entry<EnumTest, String> entry = iter.next();
System.out.println(entry.getKey().name() + ":" + entry.getValue());
}
System.out.println("=====================================================");
Iterator<Map.Entry<EnumTest, String>> iter = weekMap.entrySet().iterator();
while (iter.hasNext()){
Map.Entry<EnumTest, String> entry = iter.next();
System.out.println(entry.getKey().name() + ":" + entry.getValue());
}
}
}
3、根据输入进来的字符串,返回对应的枚举对象
public enum Day {
monday("星期一",1),
tuesday("星期二",2),
wednesday("星期三",3),
thursday("星期四",4),
friday("星期五",5),
saturday("星期六",6),
sunday("星期日",7),
星期一("monday",8),
星期二("tuesday",9),
星期三(wednesday.name,10 );
//私有属性
private String name;
private int index;
Day() {
}
//私有构造方法
Day(String name, int index) {
this.name = name;
this.index = index;
}
public int getIndex() {
return index;
}
public String getName() {
return name;
}
//根据输入进来的字符串,返回对应的枚举对象
public static Day getDay(String day){
for (Day v:values()) {
if (v.getName().equals(day)) {
return v;
}
}
return null;
}
}
4、判断是否操作成功
public enum Code {
SUCCESS(1000,"操作成功"),FAIL(1001,"操作失败");
private int code;
private String msg;
Code(int code, String msg) {
this.code = code;
this.msg = msg;
}
public int getCode() {
return code;
}
public String getMsg() {
return msg;
}
public static Code getByValue(int value){
for (Code code:values()){
if (code.getCode() ==value){
return code;
}
}
return null;
}
}
public class TestCode {
public static void main(String[] args) {
String msg = Code.SUCCESS.getMsg();
String msg1 = Code.FAIL.getMsg();
int code = 1001;
switch(Code.getByValue(code)){
case SUCCESS:
System.out.println(msg); //操作成功
break;
case FAIL:
System.out.println(msg1); //操作失败
break;
default:
System.out.println("操作有误");
}
}
}