摘要:Java Enum 操作範例
import java.util.*;
enum Language {
English("L_001"),
Mandarin("L_002"),
Spanish("L_003"),
Japanese("L_004");
Language(String key_id) {
this.key_id = key_id;
}
private final String key_id;
public String getKey_id() {
return key_id;
}
}
public class TestEnum {
public static void main(String args[]) {
/* 第一種方式 */
Set<Language> set = Collections.synchronizedSet(EnumSet.allOf(Language.class));
int size = set.size();
System.out.println("size = " + size);
Iterator it = set.iterator();
while (it.hasNext()) {
Language l = (Language) it.next();
System.out.println(l + ":\t" + l.getKey_id());
}
/* 更簡單的方式 */
System.out.println("More easy way:");
for (Language l : Language.values()) {
System.out.println(l + ":\t" + l.getKey_id());
}
}
}
Output :
----------------------------------
size = 4
English: L_001
Mandarin: L_002
Spanish: L_003
Japanese: L_004
More easy way:
English: L_001
Mandarin: L_002
Spanish: L_003
Japanese: L_004
參考資料:http://java.sun.com/docs/books/tutorial/java/javaOO/enum.html
Rocky Wang. OCUP, NCLP, SCJP, SCWCD, SCBCD