package com.test; /** * Learn the enum type * @author sangyeol.lee(korlsy@gmail.com) */ public class EnumExam { /** * old - bad code */ public static final int FONT_STYLE_NORMAL = 0; public static final int FONT_STYLE_BOLD = 1; public static final int FONT_STYLE_ITALIC = 2; /** * new - good code * 상수를 enum type으로 대체 * @author Administrator * */ public static enum FONT_STYLE{ NORMAL{ @Override public String fontStyleName() { return this.toString(); } } , BOLD{ @Override public String fontStyleName() { return this.toString(); } } , ITALIC{ @Override public String fontStyleName() { return this.toString(); } }; public abstract String fontStyleName(); } /** * @param args */ public static void main(String[] args) { /** * enum const */ //print all font style System.out.printf("font style count => %d%n", FONT_STYLE.values().length);//font style count => 3 for(FONT_STYLE style:FONT_STYLE.values()){ //style => NORMAL //style => BOLD //style => ITALIC System.out.printf("style => %s%n", style.fontStyleName()); } //good code(jdk5.0 more...) setFontStyle(FONT_STYLE.BOLD); FONT_STYLE fontStyle = getFontStyle(); switch(fontStyle){ case BOLD : /*...*/ break; case ITALIC : /*...*/ break; case NORMAL : /*...*/ break; default : /*...*/ } //bad code(jdk1.4.....) setFontStyle(FONT_STYLE_BOLD); } private static void setFontStyle(int fontStyleBold) { //.... } private static FONT_STYLE getFontStyle() { return FONT_STYLE.ITALIC; } private static void setFontStyle(FONT_STYLE bold) { //..... } }
2013년 2월 22일 금요일
java enum 을 활용하자. (learn the enum type)
howto java :: learn the enum type
피드 구독하기:
댓글 (Atom)
댓글 없음:
댓글 쓰기