åæåã使ã£ãå¦çãå ±éåãã (Java)
Javaã§è¤æ°ã®åæå(enum)ã«å¯¾ãã¦åããããªå¦çãæ¸ãå ´åã«ãã©ã®ããã«å ±éåããããèãã¾ããããã®è¨äºã§ã¯ãåæåã§å ±éã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ãå®è£ ãã¦ã対å¦ã§ããªããããªå ´åã«ã¤ãã¦æ±ãã¾ããä¾ãã°ãvalues()ã®ãããªstaticã¡ã½ãããé¢ããå ´åã§ãã対象ãã¼ã¸ã§ã³ã¯Java SE 6ã§ãã(ãã¶ã5ã§ãåãã¾ãã)
2010/08/13追è¨
è¨äºã®ã¿ã¤ãã«ããåæåã使ã£ãå¦çã®å
±éåããããåæåã使ã£ãå¦çãå
±éåãããã«å¤æ´ãã¾ããã(åè
ã ã¨ãããåæåã使ã£ãå¦çãã®å
±éåãã¨ãåæåã使ã£ããå¦çã®å
±éåããã®ããã«2種é¡ã®è§£éãã§ãã¦ç´ããããããã)
å ±éåããã¦ããªãå ´å
ã¯ããã«å ±éåããã¦ããªãå ´åã®ã³ã¼ãã示ãã¾ããmain()ãå¼ã³åºãã¨ã2種é¡ã®åæå(SizeãColor)ã«å¯¾ãã¦ãã¯ã©ã¹åã¨ã¡ã³ãä¸è¦§ãåºåãã¾ããprintSizeValues()ã¯Sizeã«å¯¾ããåºåå¦çãprintColorValues()ã¯Colorã«å¯¾ããåºåå¦çã§ãããã®2ã¤ã®ã¡ã½ããã¯ãæ±ãå以å¤ã¯å ¨ãåãã§ãã
public class EnumTest1 { private enum Size {SMALL, MEDIUM, LARGE} private enum Color {BLUE, GREEN, RED, YELLOW, WHITE} public static void main(String[] args) { printSizeValues(); printColorValues(); } private static void printSizeValues() { System.out.println("[Size]"); for (Size size : Size.values()) { System.out.println(size.name()); } System.out.println(); } private static void printColorValues() { System.out.println("[Color]"); for (Color color : Color.values()) { System.out.println(color.name()); } System.out.println(); } }
å®è¡çµæã¯æ¬¡ã®ããã«ãªãã¾ãã
[Size] SMALL MEDIUM LARGE [Color] BLUE GREEN RED YELLOW WHITE
Classãªãã¸ã§ã¯ãã使ã£ãå ±éå
ä¸è¨ã®ãå ±éåããã¦ããªãå ´åãã®printSizeValues()ã¨printColorValues()ãå ±éåããprintEnumValues()ã¨ããã¡ã½ãããä½æãã¾ããããã®ã³ã¼ãã®å®è¡çµæã¯ããå ±éåããã¦ããªãå ´åãã¨å ¨ãåãã§ãã
public class EnumTest2 { private enum Size {SMALL, MEDIUM, LARGE} private enum Color {BLUE, GREEN, RED, YELLOW, WHITE} public static void main(String[] args) { printEnumValues(Size.class); printEnumValues(Color.class); } private static void printEnumValues(Class<? extends Enum<?>> enumCls) { System.out.println("[" + enumCls.getSimpleName() + "]"); for (Enum<?> e : enumCls.getEnumConstants()) { System.out.println(e.name()); } System.out.println(); } }
è£è¶³
- å ±éã¡ã½ããã®å¼æ°ã§ãClassãªãã¸ã§ã¯ãã渡ãã¾ãã(å³å¯ã«ã¯ãClass<? extends Enum<?>>ãã§ãã)
- ã¯ã©ã¹åã®åå¾ã«ã¯ãClass#getSimpleName()ã使ç¨ãã¦ãã¾ãã
- Size#values()ãColor#values()ã®ä»£ããã«ãClass#getEnumConstants()ã使ç¨ãã¦ãã¾ãã*1
- å ±éã¡ã½ããã¯ãä»ã®ã¯ã©ã¹ã§å®ç¾©ãããã¨ãå¯è½ã§ãã(å½ç¶ã§ããâ¦)
EnumSetãªãã¸ã§ã¯ãã使ã£ãå ±éå
ä¸è¨ã®ãClassãªãã¸ã§ã¯ãã使ã£ãå ±éåãã¨ã¯å¥ã®æ¹æ³ã§ããå ±éåããã¦ããªãå ´åãã®2ã¤ã®ã¡ã½ãããå ±éåãã¾ããããã®ã³ã¼ãã®å®è¡çµæãããå ±éåããã¦ããªãå ´åãã¨å ¨ãåãã§ãã
import java.util.EnumSet; public class EnumTest3 { private enum Size {SMALL, MEDIUM, LARGE} private enum Color {BLUE, GREEN, RED, YELLOW, WHITE} public static void main(String[] args) { printEnumValues(Size.class.getSimpleName(), EnumSet.allOf(Size.class)); printEnumValues(Color.class.getSimpleName(), EnumSet.allOf(Color.class)); } private static void printEnumValues(String enumName, EnumSet<?> es) { System.out.println("[" + enumName + "]"); for (Enum<?> e : es) { System.out.println(e.name()); } System.out.println(); } }
è£è¶³
- å ±éã¡ã½ããã®å¼æ°ã§ãEnumSetãªãã¸ã§ã¯ãã渡ãã¾ãã(å³å¯ã«ã¯ãEnumSet<?>ãã§ãã)
- ã¯ã©ã¹åãªã©ã®ã¯ã©ã¹èªä½ã®æ å ±ã¯ãEnumSetãªãã¸ã§ã¯ãã¨ã¯å¥ã«æ¸¡ãå¿ è¦ãããã¾ãã*2
- å ±éã¡ã½ãããå¼ã³åºãåã«ãEnumSetãªãã¸ã§ã¯ããç·¨éãããã¨ãå¯è½ã§ãã(ç¹å®ã®å¤ã®è¿½å ã»åé¤ãªã©)
*1:ã¡ãªã¿ã«ãvalues()ã¯åå¥ã®Enumã¯ã©ã¹ã«å¯¾ãã¦ã³ã³ãã¤ã©ãèªåçæããstaticã¡ã½ããã§ãã
*2:ãã¡ããããClassãªãã¸ã§ã¯ãã使ã£ãå ±éåãã®ããã«Classã¯ã©ã¹ã®ãªãã¸ã§ã¯ãã渡ãã¦ãæ§ãã¾ããã