CORE
HOME > JAVA > J2SE > CORE
2017.05.22 / 17:21

java enum À» È°¿ëÇÏÀÚ

ducati
Ãßõ ¼ö 230

howto java :: learn the enum type

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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) {
  //.....
 }
 
}



Ãâó: http://devx.tistory.com/entry/java-enum-À»-È°¿ëÇÏÀÚ [Meta Developer Story]