본문 바로가기
major

enum class

by epro 2007. 7. 18.

예제-1 enum class

public enum Guitar {
 
 FENDER, MARTIN, GIBSON, ANY;

 public String toString(){
  switch(this) {
       case FENDER:   return "Fender";
       case MARTIN:   return "Martin";
       case GIBSON:   return "Gibson";
       default:       return "Unspecified";
     }
 }
}

@Test
 public void 기타_문자열_테스트() {
  assertEquals(Guitar.FENDER.toString(),"Fender");
  assertEquals(Guitar.MARTIN.toString(),"Martin");
 }

예제-2 enum

public class GuitarShop {
 public enum Guitar {
 
  FENDER("Fender"), MARTIN("Martin"), GIBSON("Gibson"), ANY("Unspecified");
 
  private String guitar;
 
  Guitar(String guitar) { // constructor
   this.guitar = guitar;
  }
 
  public String toString(){
   return this.guitar;
  }
 };
}

@Test
 public void 기타_생성자_문자열_테스트() throws Exception {
  assertEquals(GuitarShop.Guitar.FENDER.toString(),"Fender");
  assertEquals(GuitarShop.Guitar.MARTIN.toString(),"Martin");
 }

http://java.sun.com/docs/books/tutorial/java/javaOO/enum.html
http://java.sun.com/javase/6/docs/technotes/guides/language/enums.html

댓글