본문 바로가기
java/지네릭스, 열거형, 애너테이션

열거형 클래스

by doni73 2025. 4. 21.

열거형이란?

서로 관련된 상수를 편리하게 선언하기 위한 것
여러 상수를 정의할 때 유용!
=> 값 & 타입 체크

열거형 : 객체 갯수 제한
실드 클래스 : 자손 갯수 제한

Class Card {
    static final int CLOVER = 0;
    static final int HEART = 1;
    static final int DIAMOND = 2;
    static final int SPADE = 3;

    static final int TWO = 0;
    static final int THREE = 1;
    static final int FOUR = 2;

    final int kind;
    final int num;
}
class Card {
    enum Kind { CLOVER, HEART, DIAMOND, SPADE } //열거형 Kind를 정의
    enum Value { TWO, THREE, FOUR } //열거형 Kind를 정의

    final Kind kind; //타입이 int가 아니라 Kind!!
    final Value value;
}

-> enum Singleton { INSTANCE } ?

정의와 사용

enum 열거형이름 { 상수1, 상수2 , ... }

정의된 상수를 사용하는 방법 : 열거형이름.상수명

enum Direction { EAST, SOUTH, WEST, NORTH }
class EnumEx1 {
    public static void main(String[] args) {
        Direction d1 = Direction.EAST;
        Direction d2 = Direction.valueOf("WEST");
        Direction d3 = Enum.valueOf(Direction.class, "EAST");

        System.out.println("d1 = " + d1);
        System.out.println("d2 = " + d2);
        System.out.println("d3 = " + d3);

        System.out.println("d1==d2 ? "+ (d1 == d2));
        System.out.println("d1==d3 ? "+ (d1 == d3));
        System.out.println("d1.equals(d3) ? "+ d1.equals(d3));
        //System.out.println("d2 > d3 ? "+ (d1 > d3));
        System.out.println("d1.compareTo(d3) ? "+ (d1.compareTo(d3)));
        System.out.println("d1.compareTo(d2) ? "+ (d1.compareTo(d2)));

        switch(d1) {
            case EAST:
                System.out.println("The Direction is EAST"); break;
            case WEST:
                System.out.println("The Direction is WEST"); break;
            case NORTH:
                System.out.println("The Direction is NORTH"); break;
            case SOUTH:
                System.out.println("The Direction is SOUTH"); break;
            default:
                System.out.println("Invalid Direction"); break;
        }

        Direction[] dArr = Direction.values();

        for(Direction d : dArr)
            System.out.printf("%s = %d%n", d.name(), d.ordinal());

    }
}
d1 = EAST
d2 = WEST
d3 = EAST
d1==d2 ? false
d1==d3 ? true
d1.equals(d3) ? true
d1.compareTo(d3) ? 0
d1.compareTo(d2) ? -2
The Direction is EAST
EAST = 0
SOUTH = 1
WEST = 2
NORTH = 3

=> static 메인 메서드에서 사용 -> static보다 먼저 준비 됨

멤버 추가

private !

enum Direction {
    EAST(1, ">"), SOUTH(2,"V"), WEST(3,"<"), NORTH(4,"^");

    private static Direction[] DIR_ARR = Direction.values();
    //Direction.values() -> 열거형의 모든 상수를 배열에 담아서 반환
    private final int value;
    private final String symbol;

    Direction(int value, String symbol) {
        this.value = value;
        this.symbol = symbol;
    }
    public int getValue() { return value; }
    public String getSymbol() { return symbol; }
    public static Direction of(int value) {
        if( value < 0 || value > 4) {
            throw new IllegalArgumentException("Invlide value : " + value);
        }
        else {
            return DIR_ARR[value -1];
        }
    }
    public Direction rotate(int num) {
        num = num % 4;
        if(num <0) num += 4;
        return DIR_ARR[(value + num -1) % 4];
    }
    public String toString() { return name()+getSymbol(); }
}
class EnumEx2 {
    public static void main(String[] args) {
        for(Direction d : Direction.values()) {
            System.out.printf("%s=%s%n",d.name(),d.getValue());
        }
        Direction d1 = Direction.EAST;
        Direction d2 = Direction.of(1);

        System.out.printf("d1=%s, %d%n", d1.name(), d1.getValue());
        System.out.printf("d2=%s, %d%n", d2.name(), d2.getValue());

        System.out.println(Direction.EAST.rotate(1));
        System.out.println(Direction.EAST.rotate(2));
        System.out.println(Direction.EAST.rotate(-1));
        System.out.println(Direction.EAST.rotate(-2));
    }
}
EAST=1
SOUTH=2
WEST=3
NORTH=4
d1=EAST, 1
d2=EAST, 1
SOUTHV
WEST<
NORTH^
WEST<

'java > 지네릭스, 열거형, 애너테이션' 카테고리의 다른 글

지네릭스  (0) 2025.04.17