Chinaunix首页 | 论坛 | 博客
  • 博客访问: 468165
  • 博文数量: 1496
  • 博客积分: 79800
  • 博客等级: 大将
  • 技术积分: 9940
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-09 13:22
文章分类

全部博文(1496)

文章存档

2011年(1)

2008年(1495)

我的朋友

分类:

2008-09-09 13:30:06

  Enum是enumeration(列举)的简写形式,包含在java.lang包中。熟悉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 , 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

[1]    

【责编:Kittoy】

--------------------next---------------------

阅读(89) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~