Enums

데이터 νƒ€μž…μ„ μ •μ˜ν•΄μ£Όκ³  λ‹€ν˜•μ„±μ„ κ΅¬ν˜„ν•  수 μžˆλŠ” κΈ°λŠ₯을 μ œκ³΅ν•œλ‹€. Java의 enum은 클래슀둜 μ •μ˜λ˜μ–΄ μžˆμ–΄, λ‹¨μˆœνžˆ μ •μˆ˜ν˜• μƒμˆ˜λ₯Ό μ •μ˜ν•˜λŠ” κΈ°λŠ₯ μ΄μƒμ˜ κΈ°λŠ₯을 κ΅¬ν˜„ν•  수 μžˆλ‹€.

Enum μ •μ˜μ™€ μ‚¬μš©

κ°€μž₯ 기본적인 νƒ€μž… μ •μ˜μ™€ μ‚¬μš© 방법은 μ•„λž˜μ™€ κ°™λ‹€.

enum Direction {
    EAST, SOUTH, WEST, NORTH
}

class Unit {

    int x, y;
    Direction direction;

    void moveIf(Direction direction) {
        if (direction.equals(Direction.EAST)) { // enum의 μƒμˆ˜λŠ” equals()둜 비ꡐ κ°€λŠ₯
            x++;
        } else if (direction == Direction.SOUTH) { // '==' μ—°μ‚°μžλ₯Ό μ‚¬μš©ν•΄ equals() 보닀 λΉ λ₯Έ μ„±λŠ₯을 κΈ°λŒ€ν•  수 μžˆλ‹€.
            y--;
        } else if (direction.compareTo(Direction.NORTH)) { // compareTo() μ‚¬μš© κ°€λŠ₯
            y++;
        }
//        else if (direction > Direction.WEST) { // enum μ—λŠ” 비ꡐ μ—°μ‚°μžμΈ '<', '>' μ‚¬μš© λΆˆκ°€ 
//            x--;
//        }
    }

    void moveSwitch(Direction direction) { // switch 쑰건식 κ°€λŠ₯
        switch (direction) {
            case EAST:
                x++;
                break;
            case SOUTH:
                y--;
                break;
            case WEST:
                x--;
                break;
            case NORTH:
                y++;
                break;
        }
    }
}

μ—΄κ±°ν˜•κ³Ό 멀버 λ³€μˆ˜, λ©”μ„œλ“œ

λ‹¨μˆœνžˆ μƒμˆ˜λ₯Ό λ‚˜μ—΄ν•˜λŠ” 것 μ΄μƒμ˜ κΈ°λŠ₯을 μ œκ³΅ν•˜κΈ° μœ„ν•΄ ν΄λž˜μŠ€μ™€ λΉ„μŠ·ν•˜κ²Œ μ—΄κ±°ν˜• μƒμˆ˜μ˜ 값을 멀버 λ³€μˆ˜μ— μ €μž₯ν•  수 있고, λ©”μ„œλ“œλ₯Ό μ •μ˜ν•  수 μžˆλ‹€.

enum Direction {
    EAST(1, ">"), SOUTH(2, "V"), WEST(3, "<"), NORTH(4, "^"); // μ—΄κ±°ν˜• μƒμˆ˜λ₯Ό μ„ μ–Έκ³Ό λ™μ‹œμ— μƒμ„±μž 호좜

    private static final Direction[] DIR_ARR = Direction.values();
    private final int value;
    private final String symbol;

    private Direction(int value, String symbol) { // μ—΄κ±°ν˜•μ˜ μƒμ„±μžλŠ” private(μƒλž΅ κ°€λŠ₯)
        this.value = value;
        this.symbol = symbol;
    }

    public static Direction of(int dir) {
        if (dir < 1 || dir > 4) {
            throw new IllegalArgumentException("Invalid value : " + dir);
        }
        return DIR_ARR[dir - 1];
    }

    public int getValue() {
        return value;
    }

    public String getSymbol() {
        return symbol;
    }

    public Direction rotate(int num) {
        num = (num % 4 + 4) % 4;
        return DIR_ARR[(value - 1 + num) % 4];
    }

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

Enum과 클래슀

enum은 클래슀의 일쒅이기 λ•Œλ¬Έμ— ν΄λž˜μŠ€μ—μ„œ ν•  수 μžˆλŠ” 것듀을 ν•  수 μžˆλ‹€. μ•„λž˜λŠ” enum 내뢀에 좔상 λ©”μ„œλ“œλ₯Ό μ„ μ–Έν•˜κ³ , 각 μƒμˆ˜μ—μ„œ ν•΄λ‹Ή λ©”μ„œλ“œλ₯Ό κ΅¬ν˜„ν•˜λŠ” μ˜ˆμ‹œμ΄λ‹€.

enum Operation {
    PLUS("+") {
        @Override
        public int eval(int x, int y) {
            return x + y;
        }
    },
    MINUS("-") {
        @Override
        public int eval(int x, int y) {
            return x - y;
        }
    };

    private final String symbol;

    Operation(String symbol) {
        this.symbol = symbol;
    }

    public abstract int eval(int x, int y);

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

λ˜ν•œ, μ—΄κ±°ν˜• μƒμˆ˜μ—λŠ” λ‹¨μˆœ μ›μ‹œ νƒ€μž…μ΄λ‚˜ λ¬Έμžμ—΄λ§Œ μ €μž₯ν•  수 μžˆλŠ” 것이 μ•„λ‹ˆλΌ λžŒλ‹€μ‹μ„ μ €μž₯ν•  수 μžˆμ–΄ μ•„λž˜μ™€ 같이 λ³€κ²½ν•  수 μžˆλ‹€. κΈ°μ‘΄ λ©”μ„œλ“œλ₯Ό λžŒλ‹€μ‹μœΌλ‘œ λŒ€μ²΄ν•˜κ³  μΈν„°νŽ˜μ΄μŠ€λ₯Ό 상속받아 κ΅¬ν˜„ν•œ μœ„μ™€ λ™μΌν•œ κΈ°λŠ₯을 μˆ˜ν–‰ν•œλ‹€.

interface Calculator {

    int eval(int x, int y);
}

enum Operation implements Calculator {
    PLUS("+", (x, y) -> x + y),
    MINUS("-", (x, y) -> x - y);

    private final String symbol;
    private final IntBinaryOperator op;

    Operation(String symbol, IntBinaryOperator op) {
        this.symbol = symbol;
        this.op = op;
    }

    @Override
    public int eval(int x, int y) {
        return op.applyAsInt(x, y);
    }

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

java.lang.Enum λ©”μ„œλ“œ

Java의 λͺ¨λ“  μ—΄κ±°ν˜•μ€ java.lang.Enum 클래슀λ₯Ό μƒμ†λ°›λŠ”λ‹€. 이 ν΄λž˜μŠ€λŠ” μ—΄κ±°ν˜•μ„ 닀루기 μœ„ν•œ μ—¬λŸ¬ μœ μš©ν•œ λ©”μ„œλ“œλ₯Ό μ œκ³΅ν•œλ‹€.

λ©”μ„œλ“œμ„€λͺ…

T[] values()

λͺ¨λ“  μ—΄κ±°ν˜• μƒμˆ˜λ₯Ό λ°°μ—΄λ‘œ λ°˜ν™˜

int ordinal()

μ—΄κ±°ν˜• μƒμˆ˜μ˜ μˆœμ„œλ₯Ό λ°˜ν™˜

String name()

μ—΄κ±°ν˜• μƒμˆ˜μ˜ 이름을 λ¬Έμžμ—΄λ‘œ λ°˜ν™˜

Class getDeclaringClass()

μ—΄κ±°ν˜• μƒμˆ˜κ°€ μ •μ˜λœ μ—΄κ±°ν˜•μ˜ Class 객체λ₯Ό λ°˜ν™˜

enum Direction {
    EAST(10), SOUTH(20), WEST(30), NORTH(40);

    private final int value;

    Direction(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }
}

class Test {

    public static void main(String[] args) {
        System.out.println(Direction.EAST.getValue()); // 10
        System.out.println(Arrays.toString(Direction.values())); // [EAST, SOUTH, WEST, NORTH]
        System.out.println(Direction.EAST.ordinal()); // 0
        System.out.println(Direction.EAST.name()); // EAST
        System.out.println(Direction.EAST.getDeclaringClass()); // class Direction
    }
}

μ—΄κ±°ν˜•μ˜ λ‚΄λΆ€ κ΅¬ν˜„

enum Direction {
    EAST, SOUTH, WEST, NORTH
}

μ—΄κ±°ν˜•μ΄ μœ„μ™€ 같이 μ •μ˜λ˜μ–΄ μžˆμ„ λ•Œ 사싀은 λ‚΄λΆ€μ˜ μƒμˆ˜ ν•˜λ‚˜ν•˜λ‚˜κ°€ Direction 클래슀의 μΈμŠ€ν„΄μŠ€λΌκ³  λ³Ό 수 μžˆλ‹€. μœ„μ˜ enum을 클래슀둜 μ •μ˜ν•˜λ©΄ μ•„λž˜μ™€ 같이 ν‘œν˜„ν•  수 μžˆλ‹€.(λ™μΌν•œ 것은 μ•„λ‹˜)

class Direction {

    public static final Direction EAST = new Direction("EAST");
    public static final Direction SOUTH = new Direction("SOUTH");
    public static final Direction WEST = new Direction("WEST");
    public static final Direction NORTH = new Direction("NORTH");

    private String name;

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

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

참고자료

Last updated