java 中的 Enmu 的用法

来源:互联网 发布:如何查询端口是否开放 编辑:程序博客网 时间:2024/06/06 09:55

  枚举是java 5.0 新增的特性

  当涉及到多个常量的比较,使用枚举是很方便的

   public class EnumTest {
   public enum AttendancePatchedBillType{
    OnDutyType("1"),OffDutyType("2");

    private AttendancePatchedBillType(String value){
        this.value=value;
    }

    int getIntValue(){
        return Integer.parseInt(value);
    }
    String value;
 }
   public static void main(String[] args){
     System.out.println(AttendancePatchedBillType.OffDutyType.getIntValue());
     System.out.println(AttendancePatchedBillType.OnDutyType.getIntValue());
   }
}

 

 枚举的类型是字符串,枚举之间是用”,“隔开的,结尾是“;”

 枚举构造函数的关键字是private 的。

 其实  OnDutyType("1")是相当于 new了一次构造函数。

 枚举都是 public static final 的类型。