2012년 6월 20일 수요일

enum 사용

간단한 선언
예)
public enum Types {
 TYPE1, TYPE2, TYPE3;
}

String 값을 대입
선언 예)
public enum Types {
 TYPE1("Type 1"),
 TYPE2("Type 2"),
 TYPE3("Type 3");

 private String name;
 Types(String name) {
  this.name = name;
 }

 @Override
 public String toString() {
  return name;
 }
}

사용 예)
package com.enumtest;

public class EnumTest {
 public static void main(String[] args) throws Exception {
  Types type = Types.TYPE1;
  System.out.println(type);
 }
}

결과:
Type 1

댓글 없음:

댓글 쓰기