enum 列舉 實踐

2021-08-20 21:11:54 字數 1466 閱讀 8929

當生成乙個列舉值型別的物件時,它會直接用有兩個隱式的靜態方法

* values()

* valueof(string name)

* 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);

參考鏈結

個人覺得這兩個方法很重要,使用的場景也很多

public

enum opt

public

static opt fromvalue(int

value) else

}} // use valueof(string name) and fromvalue(int value)

public opt getopt ()

return opt;

}

public

void

withswitch (int

value)

}

列舉enum學習

enum enumeration name chongqi 3 以上簡單地按區域,將五個城市按照華南 4 華東 2 華北 3 的幾個城市分類了。4 列舉變數的定義 初始化和賦值 至於列舉可以當作特定的整型數來用的例子,從open modes可以體會。7 自定義運算子 列舉是使用者自定義型別,所以在使...

列舉 enum 總結

題外話 enum的訪問,直接寫欄位名就可以訪問 需注意作用域 區別於結構體 結構體變數.欄位名 如果乙個變數你需要幾種可能存在的值,那麼就可以被定義成為列舉型別。之所以叫列舉就是說將變數或者叫物件可能存在的情況也可以說是可能的值一一例舉出來。舉個例子來說明一吧,為了讓大家更明白一點,比如乙個鉛筆盒中...

列舉型別(enum)

enum 是計算機程式語言中的一種資料型別 列舉型別。應用場景 有些變數的取值被要求在乙個確定的範圍內,例如一周有 7天,一年 12個月,或者使用者自定義的今天安排要學習的百家姓有 4個等等。定義 在列舉型別的定義中列舉出所有的可能取值,該變數的取值只能是所列舉的範圍。格式 enum 列舉名 enu...