`
peizhiinfo
  • 浏览: 1427384 次
文章分类
社区版块
存档分类
最新评论

enum的使用

 
阅读更多

枚举常量是枚举类型中的值,即枚举值。枚举类型是一种<nobr target="_blank" style="line-height: normal; color: rgb(102, 0, 255); border-bottom-color: rgb(102, 0, 255); border-bottom-width: 1px; border-bottom-style: dotted; background-color: transparent; text-decoration: underline; ">用户</nobr>定义的类型,只有用户在程序中定义它后才能被使用。用户通常利用枚举类型定义程序中需要使用的一组相关的符号常量。枚举类型的定义格式为:

enum <枚举类型名> {<枚举表>};

它是一条枚举类型定义语句,该语句以enum保留字开始,接着为枚举类型名,它是用户命名的一个标识符,以后就直接使用它表示该类型,枚举类型名后为该类型的定义体,它是由一对花括号和其中的枚举表所组成,枚举表为一组用逗号分开的由用户命名的符号常量,每个符号常量又称为枚举常量或枚举值。如:

(1) enum color{red, yellow, blue};

(2) enum day{Sun, Mon, Tues, Wed, Thur, Fri, Sat};

<clk style="line-height: normal; "></clk>第一条语句定义了一个枚举类型color,用来表示颜色,它包含三个枚举值red,yellow和blue,分别<nobr target="_blank" style="line-height: normal; color: rgb(102, 0, 255); border-bottom-color: rgb(102, 0, 255); border-bottom-width: 1px; border-bottom-style: dotted; background-color: transparent; text-decoration: underline; ">代表</nobr>红色、黄色和兰色。

第二条语句定义了一个枚举类型day,用来表示日期,它包含7个枚举值,分别表示星期日、星期一至星期六。

<clk style="line-height: normal; "></clk>一种枚举类型被定义后,可以象整型等预定义类型一样使用在允许出现<nobr target="_blank" style="line-height: normal; color: rgb(102, 0, 255); border-bottom-color: rgb(102, 0, 255); border-bottom-width: 1px; border-bottom-style: dotted; background-color: transparent; text-decoration: underline; ">数据</nobr>类型的任何地方。如可以利用它定义变量。

(1) enum color c1, c2,c3;

(2) enum day today, workday;

(3) c1=red;

(4) workday=Wed;

第一条语句开始的保留字enum和类型标识符colou表示上述定义的枚举类型color,其中enum可以省略不写,后面的三个标识符c1,c2和c3表示该类型的三个变量,每一个变量用来表示该枚举表中列出的任一个值。

<clk style="line-height: normal; "></clk>第二条语句开始的两个<nobr target="_blank" style="line-height: normal; color: rgb(102, 0, 255); border-bottom-color: rgb(102, 0, 255); border-bottom-width: 1px; border-bottom-style: dotted; background-color: transparent; text-decoration: underline; ">成分</nobr>(成分之间的空格除外)表示上述定义的枚举类型day,同样enum可以省略不写,后面的两个标识符today和workday表示该类型的两个变量,每一个变量用来表示该枚举表中列出的七个值中的任一个值。

第三条语句把枚举值red赋给变量c1,第四条语句把枚举值Wed赋给变量workday。

<clk style="line-height: normal; "></clk>在一个枚举类型的枚举表中列出的每一个枚举常量都对应着一个整数值,该整数值可以由系统自动确认,也可以由用户<nobr target="_blank" style="line-height: normal; color: rgb(102, 0, 255); border-bottom-color: rgb(102, 0, 255); border-bottom-width: 1px; border-bottom-style: dotted; background-color: transparent; text-decoration: underline; ">指定</nobr>。若用户在枚举表中一个枚举常量后加上赋值号和一个整型常量,则就表示枚举常量被赋予了这个整型常量的值。如:

enum day{Sun=7, Mon=0, Tues, Wed, Thur, Fri, Sat};

用户指定了Sun的值为7,Mon的值为0。

<clk style="line-height: normal; "></clk>若用户没有给一个枚举常量赋初值,则<nobr target="_blank" style="line-height: normal; color: rgb(102, 0, 255); border-bottom-color: rgb(102, 0, 255); border-bottom-width: 1px; border-bottom-style: dotted; background-color: transparent; text-decoration: underline; ">系统</nobr>给它赋予的值是它前一项枚举常量的值加1,若它本身就是首项,则被自动赋予整数0。如对于上述定义的color类型,red,yellow和blue的值分别为0,1和2;对于刚被修改定义的day类型,各枚举常量的值依次为7,0,1,2,3,4,5,6。

<clk style="line-height: normal; "></clk>由于各枚举常量的值是一个整数,所以可把它同一般整数一样看待,<nobr target="_blank" style="line-height: normal; color: rgb(102, 0, 255); border-bottom-color: rgb(102, 0, 255); border-bottom-width: 1px; border-bottom-style: dotted; background-color: transparent; text-decoration: underline; ">参与</nobr>整数的各种运算。又由于它本身是一个符号常量,所以当作为输出数据项时,输出的是它的整数值,而不是它的标识符,这一点同输出其他类型的符号常量是一致的。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics