当前位置: 移动技术网 > IT编程>开发语言>Java > java枚举使用详细介绍及实现

java枚举使用详细介绍及实现

2019年07月22日  | 移动技术网IT编程  | 我要评论

java枚举使用详解

在实际编程中,往往存在着这样的“数据集”,它们的数值在程序中是稳定的,而且“数据集”中的元素是有限的。

例如星期一到星期日七个数据元素组成了一周的“数据集”,春夏秋冬四个数据元素组成了四季的“数据集”。

在java中如何更好的使用这些“数据集”呢?因此枚举便派上了用场,以下代码详细介绍了枚举的用法。

package com.ljq.test;

/**
 * 枚举用法详解
 * 
 * @author jiqinlin
 * 
 */
public class testenum {
  /**
   * 普通枚举
   * 
   * @author jiqinlin
   *
   */
  public enum colorenum {
    red, green, yellow, blue;
  }
  
  /**
   * 枚举像普通的类一样可以添加属性和方法,可以为它添加静态和非静态的属性或方法
   * 
   * @author jiqinlin
   *
   */
  public enum seasonenum {
    //注:枚举写在最前面,否则编译出错
    spring, summer, autumn, winter;

    private final static string position = "test";

    public static seasonenum getseason() {
      if ("test".equals(position))
        return spring;
      else
        return winter;
    }
  }
  
  /**
   * 性别
   * 
   * 实现带有构造器的枚举
   * 
   * @author jiqinlin
   *
   */
  public enum gender{
    //通过括号赋值,而且必须带有一个参构造器和一个属性跟方法,否则编译出错
    //赋值必须都赋值或都不赋值,不能一部分赋值一部分不赋值;如果不赋值则不能写构造器,赋值编译也出错
    man("man"), women("women");
    
    private final string value;

    //构造器默认也只能是private, 从而保证构造函数只能在内部使用
    gender(string value) {
      this.value = value;
    }
    
    public string getvalue() {
      return value;
    }
  }
  
  /**
  * 订单状态
  * 
  * 实现带有抽象方法的枚举
  * 
  * @author jiqinlin
  *
  */
  public enum orderstate {
    /** 已取消 */
    cancel {public string getname(){return "已取消";}},
    /** 待审核 */
    waitconfirm {public string getname(){return "待审核";}},
    /** 等待付款 */
    waitpayment {public string getname(){return "等待付款";}},
    /** 正在配货 */
    admeasureproduct {public string getname(){return "正在配货";}},
    /** 等待发货 */
    waitdeliver {public string getname(){return "等待发货";}},
    /** 已发货 */
    delivered {public string getname(){return "已发货";}},
    /** 已收货 */
    received {public string getname(){return "已收货";}};
    
    public abstract string getname();
  }
  
  public static void main(string[] args) {
    //枚举是一种类型,用于定义变量,以限制变量的赋值;赋值时通过“枚举名.值”取得枚举中的值
    colorenum colorenum = colorenum.blue;
    switch (colorenum) {
    case red:
      system.out.println("color is red");
      break;
    case green:
      system.out.println("color is green");
      break;
    case yellow:
      system.out.println("color is yellow");
      break;
    case blue:
      system.out.println("color is blue");
      break;
    }
    
    //遍历枚举
    system.out.println("遍历colorenum枚举中的值");
    for(colorenum color : colorenum.values()){
      system.out.println(color);
    }
    
    //获取枚举的个数
    system.out.println("colorenum枚举中的值有"+colorenum.values().length+"个");
    
    //获取枚举的索引位置,默认从0开始
    system.out.println(colorenum.red.ordinal());//0
    system.out.println(colorenum.green.ordinal());//1
    system.out.println(colorenum.yellow.ordinal());//2
    system.out.println(colorenum.blue.ordinal());//3
    
    //枚举默认实现了java.lang.comparable接口
    system.out.println(colorenum.red.compareto(colorenum.green));//-1
    
    //--------------------------
    system.out.println("===========");
    system.err.println("季节为" + seasonenum.getseason());
    
    
    //--------------
    system.out.println("===========");
    for(gender gender : gender.values()){
      system.out.println(gender.value);
    }
    
    //--------------
    system.out.println("===========");
    for(orderstate order : orderstate.values()){
      system.out.println(order.getname());
    }
  }
  
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网