JAVA基礎之枚舉

一、枚舉的做用java

若是程序運行時須要的數據只能是必定範圍內的值,jdk5以前咱們能夠採用自定義類來解決,jdk5後就能夠直接採用枚舉來解決了。設計模式

例如要限定操做類型只能爲增刪改查四種,咱們能夠用自定義類的方式解決以下:ide

public class Operation 
{  
    private String operType;
    
    private Operation(String operType)
    { 
	this.operType = operType;
    }
    
    public static final Operation ADD = new Operation("add");
    public static final Operation DELETE = new Operation("delete");
    public static final Operation MODIFY= new Operation("modify");
    public static final Operation SELECT = new Operation("select");

    public String getOperType() 
    {
        return operType;
    }

}

jdk5後咱們就能夠經過枚舉來更簡潔的解決問題:函數

 

public enum OperationByEnum
{
    ADD("add"), DELETE("delete"), MODIFY("modify"), SELECT("select");
    public String getOperType()
    {
	return operType;
    }

    private String operType;

    private OperationByEnum(String operType)
    {
	this.operType = operType;
    }

}

 查看上述兩個類的class文件能夠看出兩種方式能夠認爲是等價的,this

 

D:\java\workspace\EnumDemo\bin>javap OperationByEnum.class
Compiled from "OperationByEnum.java"
public final class OperationByEnum extends java.lang.Enum<OperationByEnum> {
  public static final OperationByEnum ADD;
  public static final OperationByEnum DELETE;
  public static final OperationByEnum MODIFY;
  public static final OperationByEnum SELECT;
  static {};
  public java.lang.String getOperType();
  public static OperationByEnum[] values();
  public static OperationByEnum valueOf(java.lang.String);
}

D:\java\workspace\EnumDemo\bin>javap Operation.class
Compiled from "Operation.java"
public class Operation {
  public static final Operation ADD;
  public static final Operation DELETE;
  public static final Operation MODIFY;
  public static final Operation SELECT;
  static {};
  public java.lang.String getOperType();
}

也就是說如今咱們定義一個enum類型後,編譯器自動幫咱們實現一個構造函數爲private自定義類,不再用咱們本身去實現一個相似於圖1中的自定義類了。spa

二、帶抽象方法的枚舉.net

 

public class EnumTest {

    public static void main(String[] args) 
    {
	print(OperationByEnum.ADD);
    }
    
    public static void print(OperationByEnum arg)
    {
	System.out.println(arg.getOperType());
    }
    
}

輸出 add

每一個枚舉值的獲取操做類型方法此時獲取的是英文名稱,若是我想添加一個方法來獲取每一個操做類型的中文名稱,應該怎麼處理呢?此時帶抽象方法的枚舉就要閃亮登場了設計

public enum OperationByEnum
{
    ADD("add")
    {

	@Override
	public String getChineseName()
	{
	    return "增";
	}
	
    }, 
    DELETE("delete")
    {

	@Override
	public String getChineseName()
	{
	    return "刪";
	}
	
    }, 
    MODIFY("modify")
    {

	@Override
	public String getChineseName()
	{
	    return "改";
	}
	
    },
   SELECT("select")
   {

    @Override
    public String getChineseName()
    {
	return "查";
    }
	
   };
    

    private String operType;
    
    public String getOperType()
    {
	return operType;
    }

    private OperationByEnum(String operType)
    {
	this.operType = operType;
    }
    
    public abstract String getChineseName();

}

此時就能夠經過getChineseName來獲取每一個枚舉值的中文名稱了。code

三、枚舉類的經常使用API對象

全部枚舉類均繼承自java.lang.Enum類,其中比較經常使用的方法爲:

下面的例子能夠幫助咱們理解valueOf方法的做用

 

String str = "ADD";
	OperationByEnum ob = OperationByEnum.valueOf(str);	
	System.out.println(ob == OperationByEnum.ADD);//TRUE
	
	String str2="add";
	OperationByEnum ob1 = OperationByEnum.valueOf(str2);
//Exception in thread "main" java.lang.IllegalArgumentException: No enum constant OperationByEnum.add

另外全部枚舉類都會有一個values()方法,用於返回枚舉的全部值

OperationByEnum[] array = OperationByEnum.values();
	for(OperationByEnum ob:array)
	{
	    System.out.println(ob.getChineseName());
	}

注:

Where does the Enum.valueOf(String) method come from?

/**
* Returns an array containing the constants of this enum 
* type, in the order they're declared.  This method may be
* used to iterate over the constants as follows:
*
*    for(E c : E.values())
*        System.out.println(c);
*
* @return an array containing the constants of this enum 
* type, in the order they're declared
*/
public static E[] values();

/**
* Returns the enum constant of this type with the specified
* name.
* The string must match exactly an identifier used to declare
* an enum constant in this type.  (Extraneous whitespace 
* characters are not permitted.)
* 
* @return the enum constant with the specified name
* @throws IllegalArgumentException if this enum type has no
* constant with the specified name
*/
public static E valueOf(String name);

四、枚舉的特性總結

枚舉類是一種特殊的JAVA類,枚舉類中每聲明一個枚舉值就表明枚舉類的一個實例對象。

與JAVA普通類同樣,聲明枚舉類時也能夠聲明類的屬性、方法、構造函數,但構造函數必須爲私有。

枚舉類也能夠實現接口,繼承抽象類。能夠做爲switch語句的參數。

若枚舉類只有一個枚舉值,則能夠當作單例設計模式使用。

相關文章
相關標籤/搜索