当前位置:文档之家› 枚举定义和语法

枚举定义和语法

枚举定义和语法
枚举定义和语法

Try{

Catch{

};

水仙花数:它是一个三位数,设cba,

A*a*a+b*b*b+c*c*c=cba

153=1*1*1+5*5*5+3*3*3

For循环

Break和continue的用法。

三元运算符:表1?表2 :表3 eg:int max = a > b ? a : b 执行过程:首先计算表达式1,表达式1应该是一个能够计算成bool类型的值,如果表达1的值为true,则表达式2的值做为整个表达式的值,如果表1的值为false,则表达式3的值作为整个表达式的值。

表达式2和表达式3的类型一定要相同。

常量:

const 类型常量名= 常量值

枚举(结构体):让我们定义一种枚举类型并且在定义这种类型时,我们要指定这个类型的所有值.

enum 自己起的类型名称{值1,值2,值3.......值n};

枚举的定义,一般和类定义在同一个级别,这样,在同一个命名空间下所有的类就都可以使用这个枚举了。(方法中)

1).限制用户不能随意赋值,只能在定义枚举时列举的值中选择. 数据库中应用

2)不需要记每一个值是什么,只需要选择相应的值

3)定义枚举时,值不能是int类型。

enum Gender

男,

Class program

......

Gender sex;

Sex=Gender.男;

MessageBox.Show(“内容”, “标题”,MessageBoxButtons. MessageBoxIcon. );

如何把一个字符串转换成枚举类型:

(Gender)(Enum.Parse(typeof(Gender),”待转换的字符串”));

枚举类型的定义

枚举类型的定义 在程序设计中,有时会用到由若干个有限数据元素组成的集合,如一周内的星期一到星期日七个数据元素组成的集合,由三种颜色红、黄、绿组成的集合,一个工作班组内十个职工组成的集合等等,程序中某个变量取值仅限于集合中的元素。此时,可将这些数据集合定义为枚举类型。因此,枚举类型是某类数据可能取值的集合,如一周内星期可能取值的集合为: { Sun,Mon,Tue,Wed,Thu,Fri,Sat} 该集合可定义为描述星期的枚举类型,该枚举类型共有七个元素,因而用枚举类型定义的枚举变量只能取集合中的某一元素值。由于枚举类型是导出数据类型,因此,必须先定义枚举类型,然后再用枚举类型定义枚举型变量。 enum <枚举类型名> { <枚举元素表> }; 其中:关键词enum表示定义的是枚举类型,枚举类型名由标识符组成,而枚举元素表由枚举元素或枚举常量组成。例如:enum weekdays { Sun,Mon,Tue,Wed,Thu,Fri,Sat }; 定义了一个名为 weekdays的枚举类型,它包含七个元素:Sun、Mon、Tue、Wed、Thu、Fri、Sat。在编译器编译程序时,

给枚举类型中的每一个元素指定一个整型常量值(也称为序号值)。若枚举类型定义中没有指定元素的整型常量值,则整型常量值从0开始依次递增,因此,weekdays枚举类型的七个元素Sun、Mon、Tue、Wed、Thu、Fri、Sat对应的整型常量值分别为0、1、2、3、4、5、6。 注意:在定义枚举类型时,也可指定元素对应的整型常量值。例如,描述逻辑值集合{TRUE、FALSE}的枚举类型boolean可定义如下: enum boolean { TRUE=1 ,FALSE=0 }; 该定义规定:TRUE的值为1,而FALSE的值为0。 而描述颜色集合{red,blue,green,black,white,yellow}的枚举类型colors可定义如下: enum colors {red=5,blue=1,green,black,white,yellow}; 该定义规定red为5 ,blue为1,其后元素值从2 开始递增加1。green、black、white、yellow的值依次为2、3、4、5。此时,整数5将用于表示二种颜色red与yellow。通常两个不同元素取相同的整数值是没有意义的。 枚举类型的定义只是定义了一个新的数据类型,只有用枚举类型定义枚举变量才能使用这种数据类型。 8.1.2枚举类型变量的定义

全面掌握java枚举类型

枚举类型是JDK5.0的新特征。Sun引进了一个全新的关键字enum来定义一个枚举类。下面就是一个典型枚举类型的定义: Java代码 1.public enum Color{ 2. RED,BLUE,BLACK,YELLOW,GREEN 3.} 显然,enum很像特殊的class,实际上enum声明定义的类型就是一个类。而这些类都是类库中Enum类的子类(https://www.doczj.com/doc/765422770.html,ng.Enum)。它们继承了这个Enum中的许多有用的方法。下面我们就详细介绍enum定义的枚举类的特征及其用法。(后面均用Color举例) 1、Color枚举类是特殊的class,其枚举值(RED,BLUE...)是Color的类对象(类实例): Color c=Color.RED; 而且这些枚举值都是public static final的,也就是我们经常所定义的常量方式,因此枚举类中的枚举值最好全部大写。 2、即然枚举类是class,当然在枚举类型中有构造器,方法和数据域。但是,枚举类的构造器有很大的不同: (1) 构造器只是在构造枚举值的时候被调用。 Java代码 1.enum Color{ 2. RED(255,0,0),BLUE(0,0,255),BLACK(0,0,0),YELLOW(2 55,255,0),GREEN(0,255,0); 3. //构造枚举值,比如RED(255,0,0) 4. private Color(int rv,int gv,int bv){ 5. this.redValue=rv; 6. this.greenValue=gv; 7. this.blueValue=bv; 8. } 9. 10. public String toString(){ //自定义的public方 法 11. return super.toString()+"("+redValue+","+greenVa lue+","+blueValue+")"; 12. } 13. 14. private int redValue; //自定义数据域,private为 了封装。 15. private int greenValue;

GrammarTest大学英语语法练习测试题

1. If the earth suddenly ________spinning, we would all fly off it. A. stopped B. had stopped C. has stopped D. would stop 2. “How should the city be run?” “If I ________a mayor, I would make the streets cleaner and hire more policemen.” A. would B. were C. would be D. should 3. If the whole operation _________ beforehand, a great deal of time and money would have been lost. A. was not planned B. has not been planned C. had not been planned D. were not planned 4. Jean doesn’t want to work right away because she thinks that if she________ a job she probably wouldn’t be able to see her friends very often. A. has to get B. were to get C. had got D. could have got 5. It is recommended that the project________ until all the preparations have been made. A. is not started B. will not be started C. not be started D. is not to be started 6. Who would you rather his daughter ________ in the same office? A. going B. to go C. have gone D. went 7. Church as we use the word refers to all religious institutions, ________they Christian, Islamic, Buddhist, Jewish, and so on. A. be B. being C. were D. are 8. My pain ________ apparent the moment I walked into the room, for the first man I met asked sympathetically:“ Are you feeling all right?” A. must be B. had been C. must have been D. had to be 9. Mary ________ my letter; otherwise she would have replied before now. A. couldn’t have received B. ought to have received C. has received D. shouldn’t have received 10. You needn’t have come over yourself. As it turned out to be a small house party, we_______ so formally. A. needn’t dress up B. did not need have dressed up C. did not need dress up D. needn’t have dressed up 11. Research findings show that we spend about two hours dreaming every night, no matter what we ________ during the day. A. should have done B. would have done C. may have done D. must have done 12. Some women ________ a good salary in a job instead of staying home, but they decided not to work for the sake of family. A. must make B. should have made C. would make D. could have made 13. He ________ another career but, at the time, he didn’t have enough money to attend graduate school. A. might have chosen B. might choose C. had to choose D. must have chosen

枚举类实现接口及在枚举类中定义抽象方法

枚举类可以实现接口,但要求枚举中的每个对象都必须覆写接口中的抽象方法。interface Print{ public String getColor() ; } enum Color implements Print{ RED{ public String getColor(){ return "红色" ; } },GREEN{ public String getColor(){ return "绿色" ; } },BLUE{ public String getColor(){ return "蓝色" ; } } ; } public class InterfaceEnumDemo{ public static void main(String args[]){ for(Color c:Color.values()){ System.out.print(c.getColor() + "、") ; } } }; 枚举类中可以定义抽象方法,但要求枚举中每个对象都必须覆写抽象方法enum Color { RED{ public String getColor(){ return "红色" ; } },GREEN{ public String getColor(){ return "绿色" ; } },BLUE{ public String getColor(){ return "蓝色" ; } } ; public abstract String getColor() ; } public class AbstractMethodEnum{

public static void main(String args[]){ for(Color c:Color.values()){ System.out.print(c.getColor() + "、") ; } } };

Grammar(语法归纳)

初中英语学习材料 madeofjingetieji Grammar(语法归纳) 一、写出下列动词的过去式 1. be ________ 2. bring ________ 3. buy ________ 4. come ________ 5. find ________ 6. drive ________ 7. fly ________ 8. give ________ 9. go ________ 10. hear ________ 11. leave ________ 12. let ________ 13. make ________ 14. put ________ 15. sing ________ 16. read ________ 17. take ________ 18. teach ________ 19. spend ________ 20. cost ________ 二、用所给词的适当形式填空。 1. Tom and Mary _________ (come) to China last month. 2. Mike __________ (not go) to bed until 12 o’clock last night. So he _______ (get) up late. 3. My mother ___________ (not do) housework yesterday. 4. She watches TV every evening. But she ___________ (not watch) TV last night. 5. ________ your father ________ (go) to work every day last year? 6. The boy_______ (not go) to school yesterday. He _______ (be) ill in bed. 7. What _________ (make) him cry just now? 8. Last year the teacher ________ (tell) us that the earth moves around the sun. 9. There ___________ (be not) any hospitals in my hometown in 1940. 10. I was afraid when I _______ (hear) a whisper. 三、完形填空。 Can flowers sing? You must be 1 to find the answer is ‘YES’. And the plants can sing, 2 . Flowers are beautiful and they 3 nice. Most people 4 to plant them. If they can sing for us, it's really wonderful. In fact, the flowers or plants in vases really can sing. 5 can they sing? That's because there is a speaker system (音响系统) inside the vases . The speaker system uses the flowers or plants to make 6 . If you like the music or song very much, you can 7 the music or the song 8 the plants in your garden. Music and plants are 9 for you .Do you like to 10

c语言中枚举类变量使用

#include void main() { printf("c语言中枚举类型变量的使用:\n"); printf("\n"); printf("实际问题说明:\n"); printf("口袋中有红、黄、蓝、白、黑5种颜色的球若干。每次从袋子中先后取出三个球,求得到3种不同颜色的球的可能取法:\n"); printf("\n"); enum color{red,yellow,blue,white,black }; enum color pri; int i,j,k,n,loop; n=0; for(i=red;i<=black;i++) { for(j=red;j<=black;j++) { if(i!=j) { for(k=red;k<=black;k++) { if((k!=i)&&(k!=j)) { n=n+1; printf("第%d个筛选组合\n",n); printf("%-4d",n); for(loop=1;loop<=3;loop++) {

switch(loop) { case 1: pri=(enum color)i;//此处需要进行强制类型转换,否则报错 break; case 2: pri=(enum color)j;//此处需要进行强制类型转换,否则报错 break; case 3: pri=(enum color)k;//此处需要进行强制类型转换,否则报错 break; default: break; } switch(pri) { case red: printf("%-10s","red"); break; case yellow: printf("%-10s","yellow"); break; case blue: printf("%-10s","blue"); break; case white: printf("%-10s","white");

头文件常用枚举类型定义

1.中断号定义 typedefenumIRQn { /****** Cortex-M4 Processor Exceptions Numbers ****************************************************************/ NonMaskableInt_IRQn = -14, /*!< 2 Non Maskable Interrupt */ MemoryManagement_IRQn = -12, /*!< 4 Cortex-M4 Memory Management Interrupt */ BusFault_IRQn = -11, /*!< 5 Cortex-M4 Bus Fault Interrupt */ UsageFault_IRQn = -10, /*!< 6 Cortex-M4 Usage Fault Interrupt */ SVCall_IRQn = -5, /*!< 11 Cortex-M4 SV Call Interrupt */ DebugMonitor_IRQn = -4, /*!< 12 Cortex-M4 Debug Monitor Interrupt */ PendSV_IRQn = -2, /*!< 14 Cortex-M4 Pend SV Interrupt */ SysTick_IRQn = -1, /*!< 15 Cortex-M4 System Tick Interrupt */ /****** STM32 specific Interrupt Numbers **********************************************************************/ WWDG_IRQn = 0, /*!< Window WatchDog Interrupt */ PVD_IRQn = 1, /*!< PVD through EXTI Line detection Interrupt */ TAMP_STAMP_IRQn = 2, /*!< Tamper and TimeStamp interrupts through the EXTI line */ RTC_WKUP_IRQn = 3, /*!< RTC Wakeup interrupt through the EXTI line */ FLASH_IRQn = 4, /*!< FLASH global Interrupt */ RCC_IRQn = 5, /*!< RCC global Interrupt */ EXTI0_IRQn = 6, /*!< EXTI Line0 Interrupt */ EXTI1_IRQn = 7, /*!< EXTI Line1 Interrupt */ EXTI2_IRQn = 8, /*!< EXTI Line2 Interrupt */ EXTI3_IRQn = 9, /*!< EXTI Line3 Interrupt */ EXTI4_IRQn = 10, /*!< EXTI Line4 Interrupt

C-枚举详解

C-枚举 在实际应用中,有的变量只有几种可能取值。如人的性别只有两种可能取值,星期只 有七种可能取值。在 C 语言中对这样取值比较特殊的变量可以定义为枚举类型。所谓枚举是指将变量的值一一列举出来,变量只限于列举出来的值的范围内取值。 定义一个变量是枚举类型,可以先定义一个枚举类型名,然后再说明这个变量是该枚 举类型。例如: enum weekday{sun,mon,tue,wed,thu,fri,sat}; 定义了一个枚举类型名 enum weekday,然后定义变量为该枚举类型。例如: enum weekday day; 当然,也可以直接定义枚举类型变量。例如: enum weekday{sun,mon,tue,wed,thu,fri,sat} day; 其中,sum,mon,…,sat 等称为枚举元素或枚举常量,它们是用户定义的标识符。 需要说明的有以下几点。 ① 枚举元素不是变量,而是常数,因此枚举元素又称为枚举常量。因为是常量,所 以不能对枚举元素进行赋值。 ② 枚举元素作为常量,它们是有值的,C 语言在编译时按定义的顺序使它们的值为 0,1,2,…。 在上面的说明中,sun 的值为 0,mon 的值为 1,…sat 的值为 6,如果有赋值语句 day=mon; 则 day 变量的值为 1。当然,这个变量值是可以输出的。例如: printf ("%d",day); 将输出整数 1。 如果在定义枚举类型时指定元素的值,也可以改变枚举元素的值。例如: enum weekday{sun=7,mon=1,tue,wed,thu,fri,sat}day; 这时,sun 为 7,mon 为 1,以后元素顺次加 1,所以 sat 就是 6 了。 ③ 枚举值可以用来作判断。例如: if (day==mon) {…} if (day>mon) {…} 枚举值的比较规则是:按其在说明时的顺序号比较,如果说明时没有人为指定,则第 一个枚举元素的值认作 0。例如,mon>sun,sat>fri。 C 语言教程 ?216? ④ 一个整数不能直接赋给一个枚举变量,必须强制进行类型转换才能赋值。例如: day=(enum weekday)2; 这个赋值的意思是,将顺序号为 2 的枚举元素赋给 day,相当于 workday=tue; 【例 11.6】从键盘输入一个整数,显示与该整数对应的枚举常量的英文名称。 # include void main( ) {

English grammar 语法概述

English grammar English grammar is the body of rules that describe the structure of expressions in the English language. This includes the structure of words, phrases, clauses, and sentences. There are historical, social, and regional variations of English. Divergences from the grammar described here occur in some dialects of English. This article describes a generalized present-day Standard English, the form of speech found in types of public discourse including broadcasting, education, entertainment, government, and news reporting, including both formal and informal speech. There are certain differences in grammar between the standard forms of British English, American English, and Australian English, although these are inconspicuous compared with the lexical and pronunciation differences. Contents [hide] ? 1 Word classes and phrases o 1.1 Nouns ? 1.1.1 Noun phrases o 1.2 Determiners o 1.3 Pronouns ? 1.3.1 Personal pronouns ? 1.3.2 Demonstrative and interrogative pronouns ? 1.3.3 Relative pronouns ? 1.3.4 There as pronoun ? 1.3.5 Other pronouns o 1.4 Verbs ? 1.4.1 Verb phrases o 1.5 Adjectives ? 1.5.1 Comparison ? 1.5.2 Adjective phrases o 1.6 Adverbs ? 1.6.1 Adverb phrases o 1.7 Prepositions o 1.8 Conjunctions ? 2 Negation ? 3 Clause and sentence structure

结构体共用体与枚举课后习题

1.定义一结构体,成员项包括一个字符型、一个整型。编程实现结构体变量成员项的输入、输出,并通过说明指针引用该变量。 #include void main() { struct a { char b; int c; }d,*p; p=&d; printf("输入:\n"); scanf("%c",&(*p).b); scanf("%d",&p->c); printf("输出:\n"); printf("%c\n",(*p).b); printf("%d\n",p->c); } 2.建立一结构体,其中包括学生的姓名、性别、年龄和一门课程的成绩。建立的结构体数组通过输入存放全班(最多45人)学生信息,输出考分最高的同学的姓名、性别、年龄和课 程的成绩。 #include void main() { int i,b,n; float a; printf("请输入班级的人数:"); scanf("%d",&n); getchar(); struct person { char name[20]; char sex[10]; int year; float score; }stu[45]; for(i=0;i

getchar(); } for(b=0,a=stu[0].score,i=0;i #define N 4 struct person { char name[20]; long int number; float score1; float score2; }; void shuru(struct person stu[N]) { int i; for(i=0;i

枚举算法

枚举算法 一、定义: 枚举法就是按问题本身的性质,一一列举出该问题所有可能的解,并在逐一列举的过程中,检验每个可能解是否是问题的真正解,若是,我们采纳这个解,否则抛弃它。在列举的过程中,既不能遗漏也不应重复。 通过生活实例,理解枚举算法的定义,找出枚举算法的关键步骤及注意点1.在枚举算法中往往把问题分解成二部分: (1)一一列举: 这是一个循环结构。要考虑的问题是如何设置循环变量、初值、终值和递增值。循环变量是否参与检验。(要强调本算法的主要是利用计算机的运算速度快这一特点,不必过多地去做算法优化工作。) (2)检验: 这是一个分支结构。要考虑的问题是检验的对象是谁?逻辑判数后的二个结果该如何处理? 2.分析出以上二个核心问题后,再合成: 要注意循环变量与判断对象是否是同一个变量。 3.该算法的输入和输出处理: 输入:大部分情况下是利用循环变量来代替。 输出:一般情况下是判断的一个分支中实现的。 用循环结构实现一一列举的过程,用分支结构实现检验的过程,理解枚举算法流程图的基本框架。 \ 二、算法实例 【例5】.求1-1000中,能被3整除的数 对该问题的分析: (1)从1-1000一一列举,这是一个循环结构 (2)在循环中对每个数进行检验。 凡是能被3整除的数,打印输出,否则继续下一个数。

【例6】.找出[1,1000]中所有能被7和11整除的数 本例参照上例,修改其中的判断部分。 【例7】.一张单据上有一个5位数的编号,万位数是1,千位数时4,百位数是7,个位数、十位数已经模糊不清。该5位数是57或67的倍数,输出所有满足这些条件的5位数的个数。 【例8】一张单据上有一个5位数的编号,万位数是1,千位数时4,十位数是7,个位数和百位数已经模糊不清。该5位数是57或67的倍数,输出所有满足这些条件的5位数的个数。 【例9】.找水仙花数(若三位数x=100a+10b+c,满足a3+b3+c3=x,则x为水仙花数) 【例10】.百鸡百钱问题(公鸡5元,母鸡3元,1元3只小鸡花100元钱,买100只鸡,怎么买?)

c#枚举(Enum)的用法及遍历方法

c#枚举(Enum)的用法及遍历方法 foreach (string s in Enum.GetNames(typeof(WallKind))) { WinFormTools.MsgBox(s); } 有人问怎样遍历Revit API中的枚举,遍历枚举是C#的语法功能。 来自MSDN 枚举可用来存储字符串与数字的值对,相当于一个对照表 常用方法:GetName(),GetValue(),Parse() using System; public class EnumTest { enum Days { Saturday, Sunday, Monday, Tuesday, Wednesday, Thursda y, Friday }; enum BoilingPoints { Celcius = 100, Fahrenheit = 212 }; [FlagsAttribute] enum Colors { Red = 1, Green = 2, Blue = 4, Yellow = 8 }; public static void Main() { Type weekdays = typeof(Days); Type boiling = typeof(BoilingPoints); Console.WriteLine("The days of the week, and their corresponding valu es in the Days Enum are:"); foreach ( string s in Enum.GetNames(weekdays) ) Console.WriteLine( "{0,-11}= {1}", s, Enum.Format( weekdays, Enu m.Parse(weekdays, s), "d")); Console.WriteLine(); Console.WriteLine("Enums can also be created which have values that r epresent some meaningful amount."); Console.WriteLine("The BoilingPoints Enum defines the following item s, and corresponding values:"); foreach ( string s in Enum.GetNames(boiling) ) Console.WriteLine( "{0,-11}= {1}", s, Enum.Format(boiling, Enum.P arse(boiling, s), "d")); Colors myColors = Colors.Red | Colors.Blue | Colors.Yellow; Console.WriteLine();

Grammar(基本语法)

Grammar 1st basic sentence pattern Everybody laughed. People suffered. The sun sets in the west. Google becomes one of the most admired companies. Ipad2 looks gorgeous! My face turned red. They fall in love. 常见系动词:look, seem, appear, sound, feel, taste, smell, grow, get, fall (ill/asleep), stand/sit (still), become, turn等Tips: of + 名词 It is of + 名词,意思等同于it is + 该名词的形容词形式,是很地道的说法。 例:This book is of great help to me. 这本书对我很有帮助。= This book is greatly helpful to me. Oil hits its highest price since September 2008. Premier Wen Jiabao made a report on the work of the government. Beijing impose d house purchase restriction policy. 常见动词总结: pass, give, show, tell, lend, take (to) buy, cook, get, sing, make (for) I gave him my address. Give me your bank card. => Give your bank card to me. buy me a big house. . => Buy a big house for me. to 表示动作方向或for 表示动作目标 I found the box empty. On Facebook, How to Keep Your Group Secret The Technology Made Mobile Payments a Reality I find it sensible 【that you are participating in the South Stream project】. 常见动词:make, keep, find, see, leave 注意:感官动词(see hear notice watch feel observe)后跟宾补,有两种形式。 动词不定式:某一次的,具体的;现在分词:经常的,反复的 I hear somebody singing somewhere. I often hear him sing in the next room.

枚举类型及Enum方法

枚举类型及Enum方法 实际问题中,有些变量的取值被限定在一个有限的范围内。例如,一个星期只有七天,一年只有十二个月。如果把这些量说明为整型,字符型或其它类型显然是不妥当的。为此,程序设计语言提供了一种枚举类型(也称为枚举)。枚举类型为定义一组可以赋给变量的命名整数常量提供了一种有效的方法。例如,使用枚举类型定义一个星期七天,定义一年十二个月: enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday } enum Months : byte { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec } 1.枚举的声明,其形式为: 访问修饰符enum 枚举名: 基础类型{ 枚举成员(标识符) } 说明:任意枚举类型都有基础类型,该基础类型可以是除char 以外的任何整型,如:byte、sbyte、short、ushort、int、uint、long 、ulong。基础类型必须能够表示该枚举中定义的所有枚举成员。枚举声明时可以对基础类型进行显式地声明。而没有显式声明时基础类型默认是int。基础类型指定为每个枚举数成员分配的内存大小。 2.枚举注意事项: 1)枚举成员不能是数值常量、字符常量、字符串常量,使用时不能加单、双引 号。例如,以下的定义是错误的: enum Days { …Sun?, …Mon?, …Tues?, …Wed?, …Thu?, …Fri?, …Sat? } enum Days { “Sun”, “Mon”, “Tues”, “Wed”, “Thu”, “Fri”, “Sat” } 2)每个枚举成员均具有相关联的常数值。此值的类型就是枚举的基础类 型。枚举类型属于顺序类型。根据定义类型时各枚举成员的排列顺序确定它们的序列,如在默认基础类型即int的情况下序列号从0开始,后面每个枚举成员的值依次递增1。当然可以显式赋值。例如: enum Days { Sunday=2, Monday, Tuesday,Wednesday, Thursday, Friday, Saturday } 3)枚举成员是常量,不是变量。尽管是标识符也不能在程序中当做变量用赋值 语句对它赋值。例如以下在程序中对枚举成员赋值会提示错误。 public class EnumTest { enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday } static void Main() { Sunday = 5; Sunday = Monday; Console.WriteLine("Sunday = {0}", Sunday); } } 4)从枚举成员到整型的转换需要用显式类型转换来完成。例如对于下面的语 句将枚举类型Days类型的枚举成员通过使用强制转换来转换枚举成员为整型。 public class EnumTest { enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }

枚举类型的定义

(一)枚举类型的定义 枚举类型是一种自定义类型,要使用枚举类型当然也要先说明枚举类型。 枚举类型的一般格式: (标识符1,标识符2,…,标识符n) 说明:①括号中的每一个标识符都称为枚举元素或枚举常量。 ②定义枚举类型时列出的所有枚举元素构成了这种枚举类型的值域(取值范围),也就是说,该类型的变量所有可能的取值都列出了。 例如,下列类型定义是合法的: type days=(sun,mon,tue,wed,thu,fri,sat); colors=(red,yellow,blue,white,black,green); 而下列类型定义是错误的(因为枚举元素非标识符): type colortype=('red','yellow','blue','white'); numbers=(1,3,5,7,9); ty=(for,do,while); (二)枚举类型变量 定义了枚举类型,就可以把某些变量说明成该类型。如: var holiday,workday:day; incolor:colors; 也可以把变量的说明与类型的定义合并在一起,如: var holiday,workday:(sun,mon,tue,wed,thu,fri,sat); incolor:(red,yellow,blue,white,black,green); (三)枚举类型的性质 ⒈枚举类型属于顺序类型 根据定义类型时各枚举元素的排列顺序确定它们的序号,第一个枚举元素的序号为0。例如:设有定义: type days=(sun,mon,tue,wed,thu,fri,sat); 则: ord(sun)=0,ord(mon)=1,ord(sat)=6;succ(sun)=mon,succ(mon)=tue, succ(fri)=sat;pred(mon)=sun,pred(tue)=mon,pred(sat)=fri。 应注意的是:枚举类型中的第一个元素无前趋,最后一个元素无后继。 ⒉对枚举类型只能进行赋值运算和关系运算

相关主题
文本预览
相关文档 最新文档