C 語言使用技巧 列舉

2021-05-23 20:46:51 字數 1091 閱讀 8846

例如有public enum animalenum

enum to string

animalenum.dog.tostring()    == dog

enum to int

int i = (int)animalenum.dog;

遍歷列舉(反射 + enum.getnames())

字串格式

type type = typeof(animalenum);

string names = enum.getnames(type);

foreach (string str in names)

列舉格式

array array = enum.getvalues(typeof(animalenum));

foreach (animalenum ae in array)

enum.fotmat           格式化輸出

public

static

string format(

type enumtype,

object value,

string format

)

enum.format(typeof(animalenum),animalenum.dog,format)

format

d or d   十進位制輸出

x or x    十六進製制輸出

g or g   字串格式輸出

f   or f   字串格式輸出

判斷列舉是否定義

enum.isdefined(typeof(animalenum),obj)

obj  = int or string

int         列舉常熟

string   列舉字串

Java列舉使用技巧

列舉是jdk 1.5中引入的新特性。對應的關鍵字為enum。其實列舉是乙個特殊的類,這個類內部儲存的是這個類的所有物件例項。列舉一般適合用於有限個數的引數定義。例如已知個數的型別定義 各種狀態的定義等。其實就是固定有限數量的全域性變數。以支付業務作為例子,定義支付單的狀態的時候,支付狀態其實有固定的...

C語言中列舉的使用

通俗來說 當我們要宣告乙個類的時候,要宣告的這個類有多個屬性,我們需要同時給多哥屬性表明屬性資訊時,使用列舉來宣告會清晰明了,方便簡潔。列舉的語法 enum 列舉名 列舉成員 列舉屬性 多個成員之間用逗號隔開,但最後乙個成員屬性不需要有任何結束符號 大括號後一定要分號 呼叫列舉方法 enum 列舉名...

C語言 列舉

在實際問題中,有些變數的取值被限定在乙個有限的範圍內。例如,乙個星期內只有七天,一年只有十二個月,乙個班每週有六門課程等等。如果把這些量說明為整型,字元型或其它型別顯然是不妥當的。為此,語言提供了一種稱為 列舉 的型別。在 列舉 型別的定義中列舉出所有可能的取值,被說明為該 列舉 型別的變數取值不能...