码迷,mamicode.com
首页 > 其他好文 > 详细

枚举类型的定义和应用

时间:2014-12-16 20:50:45      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:blog   http   ar   color   sp   on   div   log   bs   

1.枚举类型定义:

 

TYPE
  days     = (Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday);  //枚举类型,属于顺序类型,序号从0开始
  colors   =(red,yellow,blue,green,black);                               //可以同时多个枚举类型定义
  //下面定义方法是错误的
  days_e1  =(‘Sunday‘,‘Monday‘,‘Tuesday‘,‘Wednesday‘,‘Thursday‘,‘Friday‘,‘Saturday‘);   //错误:枚举类型元素只能是标识符;
  days_e2  =(1,2,3,4,5,6,7);  //错误:枚举类型元素只能是标识符;
  colors_e1=(red,yellow,white)//错误:枚举类型中元素不能和其他的元素相同;

 

2.类型类型可以用 顺数函数ord,pred,succ,low,high操作

bubuko.com,布布扣var
bubuko.com,布布扣  week  : days;
bubuko.com,布布扣  color : colors;
bubuko.com,布布扣  temp :Variant;
bubuko.com,布布扣begin
bubuko.com,布布扣//  temp:=Ord(Monday);       //返回 1
bubuko.com,布布扣//  temp:=Pred(Tuesday);     //返回 1
bubuko.com,布布扣//  temp:=Pred(Sunday);      //返回 255
bubuko.com,布布扣//  temp:=Succ(Tuesday);     //返回 3
bubuko.com,布布扣//  temp:=Succ(saturday);    //返回 7
bubuko.com,布布扣//  temp:=Low(week);         //返回 0
bubuko.com,布布扣//  temp:=High(week);        //返回 6
bubuko.com,布布扣
bubuko.com,布布扣  ShowMessage(temp);
bubuko.com,布布扣end;

 

 3.枚举类型变量只能进行赋值运算和关系运算,不能进行算术运算和逻辑运算。

bubuko.com,布布扣//合法性:
bubuko.com,布布扣  week := Sunday;
bubuko.com,布布扣  if week=Sunday then write(‘rest‘);
bubuko.com,布布扣//不合法的:
bubuko.com,布布扣  Sunday := 1;           //错把枚举值当成变量名;
bubuko.com,布布扣  days   := blue;       //枚举值blue不属于枚举变量weekday的值域;
bubuko.com,布布扣  read(color);           //枚举类型变量 不能用读语句进行赋值;
bubuko.com,布布扣  write(week); writeln(blue);//不能通过写语句输出枚举类型的变量值和枚举值。

 

4.枚举类型应用:

bubuko.com,布布扣  temp:=StrToInt(Edit1.Text);
bubuko.com,布布扣  case temp of
bubuko.com,布布扣    0:week:=Sunday;
bubuko.com,布布扣    1:week:=Monday;
bubuko.com,布布扣    2:week:=Tuesday;
bubuko.com,布布扣    3:week:=Wednesday;
bubuko.com,布布扣    4:week:=Thursday;
bubuko.com,布布扣    5:week:=Friday;
bubuko.com,布布扣    6:week:=Saturday;
bubuko.com,布布扣  end;

 

枚举类型的定义和应用

标签:blog   http   ar   color   sp   on   div   log   bs   

原文地址:http://www.cnblogs.com/delphiclub/p/4167957.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!