Posted on 2005-09-13 00:02
Justfly Shi 閱讀(816)
評論(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());
}
}
}