代码:
package hxl.calendar;
enum OpStatus{
//枚举常量定义的同时指定状态码
S_OK(0), S_PROTOCOL(1), S_AUTH(2), S_ITEMEXISTED(3), S_ITEMNOTFOUND(4), S_SQL(
15), S_UNKNOWN(16);
private int code; //状态码值
OpStatus(int code) { //非public构造方法
this.code = code;
}
@Override
public String toString() {
return String.valueOf(code);
}
}
public class aaa{
//这个方法中采用强类型的OpStatus,不要额外的检查代码
public void writeResponse(OpStatus status, String content){
System.out.println("[Status]"+status+"[Content]"+content);
}
public static void main(String[] args){
aaa test=new aaa();
test.writeResponse(OpStatus.S_OK,"good result");
test.writeResponse(OpStatus.S_ITEMEXISTED,"item existed exception");
}
}