紫风乱写

除了他眼前的屏幕,这个人什么也没看见。
被周围的电脑簇拥着,他只知道他所创造的现实,但又意识到那是虚幻。
他已经超越了技术。也超越了机器。
posts - 62, comments - 93, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

Java 5.0中的列举类型

Posted on 2005-09-13 00:02 Justfly Shi 阅读(803) 评论(0)  编辑  收藏 所属分类: Study Tiger

Enum也是java中我比较喜欢的一个改进,虽然使用到的地方并不多。
每一个enum类型都默认的继承了java.lang.Enum虚拟类。
每一个列举实例都是改enum类型的一个实例。

package cn.justfly.study.tiger.enums;

/**
 * Sample code of enum
 * 
 * @author Justfly Shi created at 2005-9-12 23:59:59
 
*/
public enum Gentle {
  WOMAN(
":)"), MAN(":|");
  Gentle(String hello) {
    _hello 
= hello;
  }

  String _hello;

  String sayHello() {
    
return _hello;
  }

  
public static void main(String[] args) {
    System.
out.println(Gentle.MAN.getDeclaringClass());

    Gentle[] allGentles 
= Gentle.values();
    System.
out.println("There are " + allGentles.length + " Gentles");
    
for (Gentle g : allGentles) {
      System.
out.println("index: " + g.ordinal() + " name: " + g.name()
          
+ " HelloSmile: " + g.sayHello());
    }
  }
}

只有注册用户登录后才能发表评论。


网站导航: