2011年3月21日月曜日

java enum

1. package test;
2.
3. /**
4. * enumのテスト
5. * @author miyasaka
6. */
7. public class EnumTest {
8.
9. enum test {
10. one ("いち", 1),
11. two ("に", 2),
12. three ("さん", 3),
13. four ("よん", 4),
14. five ("ご", 5),
15. six ("ろく", 6),
16. seven ("なな", 7),
17. eight ("はち", 8),
18. nine ("きゅう", 9),
19. ten ("じゅう", 10);
20.
21. private final String hiragana; //ひらがな
22. private final int number; // 数字
23.
24. private test(String hiragana, int number) {
25. this.hiragana = hiragana;
26. this.number = number;
27. }
28.
29. public String Hiragana() { return hiragana; }
30. public int Number() { return number; }
31. }
32.
33.
34. /**
35. * @param args
36. */
37. public static void main(String[] args) {
38.
39. int count1 = 0;
40. int count2 = 0;
41.
42. for (test t1: test.values()) {
43. System.out.println("[" + t1.ordinal() + "]:" +
44. t1.toString() + " " +
45. t1.Hiragana() + " - " +
46. t1.Number() );
47. count1 += t1.ordinal();
48. count2 += t1.Number();
49. }
50.
51. System.out.println("count1:" + count1);
52. System.out.println("count2:" + count2);
53.
54. }
55. }

0 件のコメント: