λ°μ΄ν° νμ
μ μ μν΄μ£Όκ³ λ€νμ±μ ꡬνν μ μλ κΈ°λ₯μ μ 곡νλ€.
Javaμ enumμ ν΄λμ€λ‘ μ μλμ΄ μμ΄, λ¨μν μ μν μμλ₯Ό μ μνλ κΈ°λ₯ μ΄μμ κΈ°λ₯μ ꡬνν μ μλ€.
Enum μ μμ μ¬μ©
κ°μ₯ κΈ°λ³Έμ μΈ νμ
μ μμ μ¬μ© λ°©λ²μ μλμ κ°λ€.
Copy 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;
}
}
}
μ΄κ±°νκ³Ό λ©€λ² λ³μ, λ©μλ
λ¨μν μμλ₯Ό λμ΄νλ κ² μ΄μμ κΈ°λ₯μ μ 곡νκΈ° μν΄ ν΄λμ€μ λΉμ·νκ² μ΄κ±°ν μμμ κ°μ λ©€λ² λ³μμ μ μ₯ν μ μκ³ , λ©μλλ₯Ό μ μν μ μλ€.
Copy 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 λ΄λΆμ μΆμ λ©μλλ₯Ό μ μΈνκ³ , κ° μμμμ ν΄λΉ λ©μλλ₯Ό ꡬννλ μμμ΄λ€.
Copy 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;
}
}
λν, μ΄κ±°ν μμμλ λ¨μ μμ νμ
μ΄λ λ¬Έμμ΄λ§ μ μ₯ν μ μλ κ²μ΄ μλλΌ λλ€μμ μ μ₯ν μ μμ΄ μλμ κ°μ΄ λ³κ²½ν μ μλ€.
κΈ°μ‘΄ λ©μλλ₯Ό λλ€μμΌλ‘ λ체νκ³ μΈν°νμ΄μ€λ₯Ό μμλ°μ ꡬνν μμ λμΌν κΈ°λ₯μ μννλ€.
Copy 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 ν΄λμ€λ₯Ό μμλ°λλ€. μ΄ ν΄λμ€λ μ΄κ±°νμ λ€λ£¨κΈ° μν μ¬λ¬ μ μ©ν λ©μλλ₯Ό μ 곡νλ€.
λͺ¨λ μ΄κ±°ν μμλ₯Ό λ°°μ΄λ‘ λ°ν
μ΄κ±°ν μμμ μμλ₯Ό λ°ν
μ΄κ±°ν μμμ μ΄λ¦μ λ¬Έμμ΄λ‘ λ°ν
Class getDeclaringClass()
μ΄κ±°ν μμκ° μ μλ μ΄κ±°νμ Class κ°μ²΄λ₯Ό λ°ν
Copy 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
}
}
μ΄κ±°νμ λ΄λΆ ꡬν
Copy enum Direction {
EAST, SOUTH, WEST, NORTH
}
μ΄κ±°νμ΄ μμ κ°μ΄ μ μλμ΄ μμ λ μ¬μ€μ λ΄λΆμ μμ νλνλκ° Direction
ν΄λμ€μ μΈμ€ν΄μ€λΌκ³ λ³Ό μ μλ€.
μμ enumμ ν΄λμ€λ‘ μ μνλ©΄ μλμ κ°μ΄ ννν μ μλ€.(λμΌν κ²μ μλ)
Copy 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 6 months ago