π¦μ΄κ±°ν(enums)
- 1.1 μ΄κ±°νμ΄λ?
- 1.2 μ΄κ±°νμ μ μμ μ¬μ©
- 1.3 μ΄κ±°νμ λ©€λ² μΆκ°νκΈ°
- 1.4 μ΄κ±°νμ μ΄ν΄
π΅1.1 μ΄κ±°νμ΄λ?
μ»΄ν¨ν° νλ‘κ·Έλλ°μμ μ΄κ±°νμ΄λ μμ, λ©€λ²λΌ λΆλ¦¬λ λͺ λͺ λ κ°μ μ§ν©μ μ΄λ£¨λ μλ£νμ΄λ€.
μλ°μ μ΄κ±°νμ μ΄κ±°νμ΄ κ°λ κ°λΏλ§ μλλΌ νμ κΉμ§ κ΄λ¦¬νκΈ° λλ¬Έμ λ³΄λ€ λ Όλ¦¬μ μΈ μ€λ₯λ₯Ό μ€μΌ μ μλ€.
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 } //μ΄κ±°ν Value μ μ
final Kind kind; // νμ
μ΄ int κ° μλ Kind μ΄λ€
final Value value;
}
κΈ°μ‘΄μ λ§μ μΈμ΄λ€μμλ νμ μ΄ λ¬λΌλ κ°μ΄ κ°μΌλ©΄ 쑰건μ κ²°κ³Όκ° μ°Έ(true)μ΄μμΌλ μλ°μ 'νμ μ μμ ν μ΄κ±°ν(typesafe enum)' μμλ μ€μ κ°μ΄ κ°μλ νμ μ΄ λ€λ₯΄λ©΄ 쑰건μμ κ²°κ³Όκ° falseκ° λλ€.
κΈ°μ‘΄
if(Card.CLOVER == Card.TWO) // μλ―Έμ false μ΄μ§λ§, κ° 0μΌλ‘ κ°μ true λ‘ λμ΄
enum νμ©
if(Card.Kind.CLOVER == Card.Value.TWO) // κ°μ΄ 0μΌλ‘ λμΌ νμ§λ§, μλ―Έλ λ¬λΌ falseλ‘ λμ¨λ€
μ΄μ²λΌ κ°λΏλ§ μλλΌ νμ κΉμ§ 체ν¬νκΈ° λλ¬Έμ νμ μ μμ νλ€κ³ νλ κ²μ΄λ€.
κ·Έλ¦¬κ³ λ μ€μν κ²μ μ΄κ±°ν μμλ₯Ό μ¬μ©νλ©΄, κΈ°μ‘΄μ μμ€λ₯Ό λ€μ μ»΄νμΌ νμ§ μμλ λλ€. ( κΈ°μ‘΄μλ ν΄λΉ μμλ₯Ό μ°Έμ‘°νλ λͺ¨λ μμ€λ₯Ό λ€μ μ»΄νμΌ ν΄μΌ νλ€.)
π΅1.2 μ΄κ±°νμ μ μμ μ¬μ©
μ΄κ±°νμ μ μνλ λ°©λ²μ κ°λ¨νλ€. μ€κ΄νΈ{} μμ μμμ μ΄λ¦μ λμ΄νκΈ°λ§ νλ©΄ λλ€.
enum μ΄κ±°νλͺ { μμλͺ 1, μμλͺ 2, ... }
μμ λ‘ λμλ¨λΆ 4λ°©ν₯μ μμλ‘ μ μνλ μ΄κ±°ν Directionμ μμ±ν΄λ³΄μ.
enum Direction { EAST, SOUTH, WEST, NORTH }
μ΄ μ΄κ±°νμ μ μλ μμλ₯Ό μ¬μ©νλ λ°©λ²μ 'μ΄κ±°νλͺ .μμλͺ ' μ΄λ€. ν΄λμ€μ static λ³μλ₯Ό μ°Έμ‘°νλ κ²κ³Ό λμΌνλ€.
class Unit{
int x,y; //μ λ μμΉ
Direction dir; // μ λ λ°©ν₯. μ΄κ±°νμ μΈμ€ν΄μ€ λ³μλ‘ μ μΈ
void init(){
dir = Direction.EAST; // μ λμ λ°©ν₯μ EASTλ‘ μ΄κΈ°ν
}
}
μ΄κ±°ν μμκ°μ λΉκ΅μλ == λ₯Ό μ¬μ©ν μ μλ€. equals()κ° μλ == λ‘ λΉκ΅κ° κ°λ₯νλ€λ κ²μ κ·Έλ§νΌ λΉ λ₯Έ μ±λ₯μ μ 곡νλ€λ λ§μ΄λ€. κ·Έλ¬λ > < κ°μ λΉκ΅μ°μ°μλ μ¬μ©ν μ μκ³ , compareTo()λ μ¬μ©κ°λ₯νλ€.
( compareTo() λ λ λΉκ΅λμμ΄ κ°μΌλ©΄ 0, μΌμͺ½μ΄ ν¬λ©΄ μμ, μ€λ₯Έμͺ½μ΄ ν¬λ©΄ μμλ₯Ό λ°ννλ€. )
if(dir == Direction.EAST){
x++;
} else if (dir > Direction.WEST){ //μλ¬. μ΄κ±°ν μμμ λΉκ΅μ°μ°μ μ¬μ© λΆκ°
...
} else if (dir.compareTo(Direction.WEST) > 0){ // compareTo()λ μ¬μ© κ°λ₯νλ€.
...
}
λ€μκ³Ό κ°μ΄ switch λ¬ΈμΌλ‘λ κ°λ₯νλ€.
void move(){
switch(dir){
case EAST: x++;
break;
case WEST: x--;
break;
case SOUTH: y++;
break;
case NORTH: y--;
break;
}
}
μ΄λλ caseλ¬Έμ μ΄κ±°ν μ΄λ¦μ μ μ§ μκ³ , μμλͺ λ§ μ μ΄μΌ νλ€λ μ μ½μ΄ μλ€.
λͺ¨λ μ΄κ±°νμ μ‘°μ - java.lang.Enum
μ΄κ±°ν Directionμ μ μλ λͺ¨λ μμλ₯Ό μΆλ ₯νλ €λ©΄, λ€μκ³Ό κ°μ΄νλ€.
Direction[] dArr = Direction.values();
for(Direction d : dArr) // Direction.values() λͺ¨λ μμμ κ°μ λ΄μ dArr 루ν
System.out.printf("%s = %d%n",d.name(),d.ordinal());
ordinal() μ λͺ¨λ μ΄κ±°νμ μ‘°μμΈ java.lang.Enumν΄λμ€μ μ μλ κ²μΌλ‘, μ΄κ±°ν μμκ° μ μλ μμ(0λΆν° μμ)λ₯Ό μ μλ‘ λ°ννλ€.
λ©μλ | μ€λͺ |
Class<E> getDeclaringClass() | μ΄κ±°νμ Class κ°μ²΄λ₯Ό λ°ννλ€. |
String name() | μ΄κ±°ν μμμ μ΄λ¦μ λ¬Έμμ΄λ‘ λ°ννλ€. |
int ordinal() | μ΄κ±°ν μμκ° μ μλ μμλ₯Ό λ°ννλ€.(0λΆν° μμ) |
T valueOf(Class<T> enumType, String name) | μ§μ λ μ΄κ±°νμμ nameκ³Ό μΌμΉνλ μ΄κ±°ν μμλ₯Ό λ°ννλ€. |
valueOf() λ μ΄κ±°ν μμμ μ΄λ¦μ μ΄μ©νμ¬ λ¬Έμμ΄ μμμ λν μ°Έμ‘°λ₯Ό μ»μμμλ€.
Direction d = Direction.valueOf("WEST");
System.out.println(d); // WEST
System.out.println(Direction.WEST==Direction.valueOf("WEST")); //true
μ΄μ μμ λ₯Ό ν΅ν΄ μ΄κ±°νμ μ§μ μ μνκ³ μ¬μ©ν΄λ³΄μ.
public class RoadToJavaApplication {
enum Direction { EAST, SOUTH, WEST, NORTH }
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)); //μ΄κ±°ν μμκ°μ λΉκ΅λ ==μ°μ° κ°λ₯. κ²°κ³Ό false
System.out.println("d1==d3 : "+(d1==d3)); //μ΄κ±°ν μμκ°μ λΉκ΅λ ==μ°μ° κ°λ₯. κ²°κ³Ό true
System.out.println("d1.equals(d3) : "+(d1.equals(d3))); //equalsλ‘ λΉκ΅ κ°λ₯. ==λ³΄λ€ λλ¦° μ±λ₯
// System.out.println("d1 > d3) : "+(d1>d3)); // > < λΉκ΅μ°μ° μ¬μ© λΆκ°
System.out.println("d1.compareTo(d3) : "+d1.compareTo(d3)); // a.compareTo(b) a,b λΉκ΅ νμ¬ μΌμ΄ ν¬λ©΄ μμ,κ°μΌλ©΄ 0, μ€λ₯Έμ΄ ν¬λ©΄ μμ
System.out.println("d1.compareTo(d2) : "+d1.compareTo(d2)); // a.compareTo(b) a,b λΉκ΅ νμ¬ μΌμ΄ ν¬λ©΄ μμ,κ°μΌλ©΄ 0, μ€λ₯Έμ΄ ν¬λ©΄ μμ
/* switch μ°μ° */
switch (d1){
case EAST: //Direction.EAST λΌκ³ μΈ μ μλ€.
System.out.println("Direction is EAST");break;
case WEST:
System.out.println("Direction is WEST");break;
case SOUTH:
System.out.println("Direction is SOUTH");break;
case NORTH:
System.out.println("Direction is NORTH");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
Direction is EAST
EAST = 0
SOUTH = 1
WEST = 2
NORTH = 3
π΅1.3 μ΄κ±°νμ λ©€λ² μΆκ°νκΈ°
Enum ν΄λμ€μ μ μλ ordinal()μ΄ μ΄κ±°ν μμκ° μ μλ μμλ₯Ό λ°ννμ§λ§, μ΄ κ°μ μ΄κ±°ν μμμ κ°μΌλ‘ μ¬μ©νμ§ μλκ²μ΄ μ’λ€. μ΄ κ°μ λ΄λΆμ μΈ μ©λλ‘λ§ μ¬μ©λκΈ° λλ¬Έμ΄λ€.
μ΄κ±°νμ μμκ°μ΄ μ°μμ μ΄λΌλ©΄ μκ΄μμ§λ§, λΆμ°μμ μΈ κ²½μ°μλ μ΄λ¦ μμ μνλ κ°μ κ΄νΈ()μ ν¨κ» μ μ΄μ£Όλ©΄ λλ€.
enum Direction {
EAST(1), SOUTH(5), WEST(-1), NORTH(10); //λμ ; ν΄μ£Όμ΄μΌν¨
private final int value;//μΈμ€ν΄μ€ λ³μ
Direction(int value) { //μμ±μ
this.value = value;
}
public int getValue() {
return value;
}
}
μ§μ λ κ°μ μ μ₯ν μ μλ μΈμ€ν΄μ€ λ³μμ μμ±μλ₯Ό μλ‘ μΆκ°ν΄ μ£Όμ΄μΌ νλ€.
μ΄κ±°νμ μΈμ€ν΄μ€ λ³μλ finalμ΄μ΄μΌ νλ€λ μ μ½μ μμ§λ§, valueλ μ΄κ±°ν μμμ κ°μ μ μ₯νκΈ° μν κ²μ΄λ―λ‘ finalμ λΆμ.
Direction d = new Direction(1); // μλ¬. μ΄κ±°νμ μμ±μλ μΈλΆμμ νΈμΆ λΆκ°
μ΄κ±°ν Directionμ μλ‘μ΄ μμ±μκ° μΆκ° λμμ§λ§, μμ κ°μ΄ μ΄κ±°νμ κ°μ²΄λ₯Ό μμ±ν μ μλ€. μ΄κ±°νμ μμ±μλ μ μ΄μκ° λ¬΅μμ μΌλ‘ privateμ΄κΈ° λλ¬Έμ΄λ€.
enum Direction{
...
Direction(inte value) { // private Direction(inte value)μ λμΌ
...
}
}
νμνλ€λ©΄ , μλμ κ°μ΄ νλμ μ΄κ±°ν μμμ μ¬λ¬ κ°μ μ§μ ν μ μλ€. λ€λ§ κ·Έμ λ§κ² μΈμ€ν΄μ€ λ³μμ μμ±μ λ±μ μλ‘ μΆκ°ν΄μ£Όμ΄μΌ νλ€.
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; //μΈμ€ν΄μ€ λ³μ μΆκ°
Direction(int value,String symbol){ // private
this.value = value;
this.symbol = symbol;
}
public int getValue(){return value;}
public String getSymbol(){return symbol;}
public static Direction of(int dir){
if(dir < 1 || dir >4){
throw new IllegalArgumentException("Invalid value :"+dir);
}
return DIR_ARR[dir-1];
}
//λ°©ν₯μ νμ μν€λ λ©μλ. numμ κ°λ§νΌ 90λμ© μκ³λ°©ν₯μΌλ‘ νμ νλ€.
public Direction rotate(int num){
num = num % 4;
if(num<0) num+=4; //numμ΄ μμμΌ λλ μκ³ λ°λ λ°©ν₯μΌλ‘ νμ
return DIR_ARR[(value-1+num)%4];
}
public String toString(){
return name()+getSymbol();
}
}
class EnumEx2 {
@Test
void main(){
for(Direction d: Direction.values())
System.out.printf("%s=%d%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<
μ΄κ±°νμ μΆμ λ©μλ μΆκ°νκΈ°
μλμ μ΄κ±°ν Transportationμ μ΄μ‘ μλ¨μ μ’ λ₯ λ³λ‘ μμλ₯Ό μ μνκ³ μμΌλ©°, κ° μ΄μ‘ μλ¨μλ κΈ°λ³ΈμκΈ(BASIC_FARE)μ΄ μ± μ λμ΄ μλ€.
enum Transportation {
BUS(100), TRAIN(150), SHIP(100), AIRPLANE(300);
private final int BASIC_FARE;
private transportation(int basicFare) {
BASIC_FARE = basicFare;
}
int fare() { //μ΄μ‘ μκΈμ λ°ν
return BASIC_FARE;
}
}
κ·Έλ¬λ μ΄κ²λ§μΌλ‘λ λΆμ‘±νλ€.
π΅1.4 μ΄κ±°νμ μ΄ν΄
'Java | Spring > Java' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[JAVA] 컬λ μ νλ μμν¬ - μ΄ν°λ μ΄ν° (0) | 2021.06.21 |
---|
λκΈ