分享

枚举类型的声明及应用

 幸福的落叶@ing 2010-09-25

枚举类型的定义和枚举变量的说明

  1. 枚举的定义枚举类型定义的一般形式为:
  enum 枚举名{ 枚举值表 };
  在枚举值表中应罗列出所有可用值。这些值也称为枚举元素。
  例如:
  该枚举名为weekday,枚举值共有7个,即一周中的七天。凡被说明为weekday类型变量的取值只能是七天中的某一天。
  2. 枚举变量的说明
  如同结构和联合一样,枚举变量也可用不同的方式说明,即先定义后说明,同时定义说明或直接说明。
  设有变量a,b,c被说明为上述的weekday,可采用下述任一种方式:
  enum weekday{ sun,mon,tue,wed,thu,fri,sat };
  enum weekday a,b,c;
  或者为:
  enum weekday{ sun,mon,tue,wed,thu,fri,sat }a,b,c;
  或者为:
  enum { sun,mon,tue,wed,thu,fri,sat }a,b,c;

枚举类型变量的赋值和使用

  枚举类型在使用中有以下规定:
  1. 枚举值是常量,不是变量。不能在程序中用赋值语句再对它赋值。
  例如对枚举weekday的元素再作以下赋值:
  sun=5;
  mon=2;
  sun=mon;
  都是错误的。
  2. 枚举元素本身由系统定义了一个表示序号的数值,从0开始顺序定义为0,1,2…。如在weekday中,sun值为0,mon值为1,…,sat值为6。
  【例11.10】
  main(){
  enum weekday
  { sun,mon,tue,wed,thu,fri,sat } a,b,c;
  a=sun;
  b=mon;
  c=tue;
  printf("%d,%d,%d",a,b,c);
  }
  说明:
  只能把枚举值赋予枚举变量,不能把元素的数值直接赋予枚举变量。如:
  a=sun;
  b=mon;
  是正确的。而:
  a=0;
  b=1;
  是错误的。如一定要把数值赋予枚举变量,则必须用强制类型转换。
  如:
  a=(enum weekday)2;
  其意义是将顺序号为2的枚举元素赋予枚举变量a,相当于:
  a=tue;
  还应该说明的是枚举元素不是字符常量也不是字符串常量,使用时不要加单、双引号。
  【例11.11】
  #include <stdio.h>
  int main(){
  enum body
  { a,b,c,d } month[31],j;
  int i1;
  j=a;
  for(i1=1;i1<=30;i1++){
  month[i1]=j;
  j++;
  if (j>d) j=a;
  }
  for(i1=1;i1<=30;i1++){
  switch(month)
  {
  case a:printf(" %2d %c\t",i1,'a'); break;
  case b:printf(" %2d %c\t",i1,'b'); break;
  case c:printf(" %2d %c\t",i1,'c'); break;
  case d:printf(" %2d %c\t",i1,'d'); break;
  default:break;
  }
  }
  printf("\n");
  }

Enum的用法

  Enum是enumeration(列举)的简写形式,包含在java.lang包中.熟悉C, C++, C#, 或 Pascal人应该对列举有所了解,先看个例子: public enum Season { winter, spring, summer, fall }
  一个enum是定义一组值的对象,它可以包括零个或多个值成员.它是属于enum类型的,一个enum对象中不可有两个或多个相同的属性或值.在次之前的java程序员一般是 用接口的方法实现列举的,如 :
  public interface Season {
  static winter = 0;
  static spring = 1; //etc..
  }
  引入了enum的java的列举的编写方便了许多,只须定义一个enum型的对象.enum对象的值都回自动获得一个数字值,从0开始,依次递增.看一个比较简单的enum实现的例子:
  EnumDemo.java
  package net.javagarage.enums;
  /*
  We can loop over the values we put into the enum
  using the values() method.
  Note that the enum Seasons is compiled into a
  separate unit, called EnumDemo$Seasons.class
  */
  public class EnumDemo {
  /*declare the enum and add values to it. note that, like in C#, we don't use a ; to
  end this statement and we use commas to separate the values */
  private enum Seasons { winter, spring,
  summer, fall }
  //list the values
  public static void main(String[] args) {
  for (Seasons s : Seasons.values()){
  System.out.println(s);
  }
  }
  }
  运行上述代码你回得到 以下结果:
  winter
  spring
  summer
  fall
  Enum的属性调用:
  下面的代码展示了调用enum对象的方法,这也是它通常的用法:
  package net.javagarage.enums;
  /*
  File: EnumSwitch.java
  Purpose: show how to switch against the values in an enum.
  */
  public class EnumSwitch {
  private enum Color { red, blue, green }
  //list the values
  public static void main(String[] args) {
  //refer to the qualified value
  doIt(Color.red);
  }
  /*note that you switch against the UNQUALIFIED name. that is, "case Color.red:" is a
  compiler error */
  private static void doIt(Color c){
  switch (c) {
  case red:
  System.out.println("value is " + Color.red);
  break;
  case green:
  System.out.println("value is " + Color.green);
  break;
  case blue:
  System.out.println("value is : " + Color.blue);
  break;
  default :
  System.out.println("default");
  }
  }
  }
  为enums添加属性和方法
  enums也可以象一般的类一样添加方法和属性,你可以为它添加静态和非静态的属性或方法,这一切都象你在一般的类中做的那样.
  package net.javagarage.enums;
  /*
  File: EnumDemo.java
  Purpose: show how to use an enum that also defines its own fields and methods
  */
  public class EnumWithMethods {
  //declare the enum and add values to it.
  public enum Season {
  winter, spring, summer, fall;
  private final static String location = "Phoenix";
  public static Season getBest(){
  if (location.equals("Phoenix"))
  return winter;
  else
  return summer;
  }
  public static void main(String[] args) {
  System.out.println(Season.getBest());
  }
  }
  就是这么的简单.但是有一点是需要注意的,那就是enums的值列表必须紧跟在enum声明,不然编译时将会出错.
  Enums构造函数:
  和类一样enums也可以有自己的构造函数,如下:
  package net.javagarage.enums;
  public class EnumConstructor {
  public static void main(String[] a) {
  //call our enum using the values method
  for (Temp t : Temp.values())
  System.out.println(t + " is : " + t.getValue());
  }
  //make the enum
  public enum Temp {
  absoluteZero(-459), freezing(32),
  boiling(212), paperBurns(451);
  //constructor here
  Temp(int value) {
  this.value = value;
  }
  //regular field?but make it final,
  //since that is the point, to make constants
  private final int value;
  //regular get method
  public int getValue() {
  return value;
  }
  }
  }
  输出结果是:
  absoluteZero is : -459
  freezing is : 32
  boiling is : 212
  paperBurns is : 451
  尽管enums有这么多的属性,但并不是用的越多越好,如果那样还不如直接用类来的直接.enums的优势在定义int最终变量仅当这些值有一定特殊含义时.但是如果你需要的是一个类,就定义一个类,而不是enum.

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多