java enum列舉使用例子
package com.wql.test;
public class Test6 {
public static void main(String[] args) {
test1(ErrorType.ERROR_2);
}
public static void test1(ErrorType type){
switch(type){
case ERROR_1:
System.out.println("引數type="+type+",value="+type.getValue()+",msg="+type.getMsg());
break;
case ERROR_2:
System.out.println("引數type="+type+",value="+type.getValue()+",msg="+type.getMsg());
break;
case ERROR_3:
System.out.println("引數type="+type+",value="+type.getValue()+",msg="+type.getMsg());
break;
}
}
}
enum ErrorType{
ERROR_1(1,"一錯誤"),
ERROR_2(2,"二錯誤"),
ERROR_3(3,"三錯誤");
private int value;//錯誤程式碼
private String msg;//錯誤描述
//注意!構造方法是私有的
private ErrorType(int value,String msg){
this.value = value;
this.msg = msg;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
為了讓學習變得輕鬆、高效,今天給大家免費分享一套Java教學資源。幫助大家在成為Java架構師的道路上披荊斬棘。需要資料的歡迎加入學習交流群:9285,05736
