"Java enum"의 두 판 사이의 차이

78번째 줄: 78번째 줄:
         case DAY.WEDNESDAY:
         case DAY.WEDNESDAY:
             System.out.println("수요일 입니다.");
             System.out.println("수요일 입니다.");
            break;
        }
    }
}
</syntaxhighlight>
*enum을 사용하여 다시 표현
<syntaxhighlight lang='java' run>
enum Day{ 
    MONDAY,TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
}
enum Month{ 
    JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY,
    AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER;
}
public class EnumExample3 {
    public static void main(String[] args) {       
        Day day = Day.MONDAY;
        switch (day) {
        case MONDAY:
            System.out.println("월요일입니다.");
            break;
        case TUESDAY:
            System.out.println("화요일입니다.");
            break;
        case WEDNESDAY:
            System.out.println("수요일입니다.");
             break;
             break;
         }
         }

2021년 8월 22일 (일) 19:48 판

개요

열거형상수
  • 예제 그냥 상수로 1주일을 표현했을때
public class EnumExample1 {

    private final static int MONDAY = 1;
    private final static int TUESDAY = 2;
    private final static int WEDNESDAY = 3;
    private final static int THURSDAY = 4;
    private final static int FRIDAY = 5;
    private final static int SATURDAY = 6;
    private final static int SUNDAY = 7;

    public static void main(String[] args) {        

        int day = MONDAY;

        switch (day) {
        case MONDAY:
            System.out.println("월요일 입니다.");
            break;
        case TUESDAY:
            System.out.println("화요일 입니다.");
            break;
        case WEDNESDAY:
            System.out.println("수요일 입니다.");
            break;
        }
    }
}
  • interface를 이용한 방법
interface DAY{  
    int MONDAY = 1;
    int TUESDAY = 2;
    int WEDNESDAY = 3;
    int THURSDAY = 4;
    int FRIDAY = 5;
    int SATURDAY = 6;
    int SUNDAY = 7;
}

interface MONTH{  
    int JANUARY = 1;
    int FEBRUARY = 2;
    int MARCH = 3;
    int APRIL = 4;
    int MAY =5;
    int JUNE = 6;
    int JULY = 7;
    int AUGUST = 8;
    int SEPTEMBER = 9;
    int OCTOBER = 10;
    int NOVEMBER = 11;
    int DECEMBER = 12;
}

public class EnumExample2 {

    public static void main(String[] args) {

        if(DAY.MONDAY == MONTH.JANUARY){ //이부분에러
            System.out.println("두 상수는 같습니다.");
        }       

        int day = DAY.MONDAY;

        switch (day) {
        case DAY.MONDAY:
            System.out.println("월요일 입니다.");
            break;
        case DAY.TUESDAY:
            System.out.println("화요일 입니다.");
            break;
        case DAY.WEDNESDAY:
            System.out.println("수요일 입니다.");
            break;
        }
    }
}
  • enum을 사용하여 다시 표현
enum Day{  
    MONDAY,TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
}

enum Month{  
    JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, 
    AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER;
}

public class EnumExample3 {

    public static void main(String[] args) {        

        Day day = Day.MONDAY;

        switch (day) {
        case MONDAY:
            System.out.println("월요일입니다.");
            break;
        case TUESDAY:
            System.out.println("화요일입니다.");
            break;
        case WEDNESDAY:
            System.out.println("수요일입니다.");
            break;
        }
    }
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}