JDK 1.5에 추가된 기능.
enum Direction { EAST, WEST, ...}
열거형 상수의 이름 옆에 원하는 값을 괄호와 함꼐 적어주면 된다.
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;
}
}
열거형에 추상 메서드를 선언하면 각 열거형 상수가 이 추상 메서드를 반드시 구현해야 한다. 필드값을 protected로 해야 메소드에서 각 상수에 접근이 가능하다.
enum Direction {
EAST(1) {
@Override
int cost(int distance) {
return distance * value;
}
},
SOUTH(5) {
@Override
int cost(int distance) {
return distance * value;
}
},
WEST(-1) {
@Override
int cost(int distance) {
return distance * value;
}
},
NORTH(10) {
@Override
int cost(int distance) {
return distance * value;
}
};
abstract int cost(int distance);
protected final int value;
Direction(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
내부적인 열거형의 구현
class Direction {
public static void main(String[] args) {
System.out.println(Direction.EAST.getName());
}
static final Direction EAST = new Direction("EAST");
static final Direction SOUTH = new Direction("SOUTH");
static final Direction WEST = new Direction("WEST");
static final Direction NORTH = new Direction("NORTH");
private String name;
private Direction(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
abstract class MyEnum<T extends MyEnum<T>> implements Comparable<T>{
static int id = 0;
int oridinal;
String name = "";
public int oridinal(){ return oridinal; }
MyEnum(String name){
this.name = name;
oridinal = id++;
}
public int compareTo(T t){
return oridinal - t.oridinal();
}
}
abstract class MyTransportation extends MyEnum {
static final MyTransportation BUS = new MyTransportation("BUS", 100) {
int fare(int distance) { return distance - BASIC_FARE; }
};
static final MyTransportation TRAIN = new MyTransportation("TRAIN", 150) {
int fare(int distance) { return distance - BASIC_FARE; }
};
static final MyTransportation SHIP = new MyTransportation("SHIP", 100) {
int fare(int distance) { return distance - BASIC_FARE; }
};
static final MyTransportation AIRPLANE = new MyTransportation("AIRPLANE", 300) {
int fare(int distance) { return distance - BASIC_FARE; }
};
abstract int fare(int distance); // 거리에 따른 요금을 계산하는 추상 메서드
protected final int BASIC_FARE; // protected로 해야 각 상수에서 접근 가능
private MyTransportation(String name, int basicFare){
super(name);
BASIC_FARE = basicFare;
}
public String name() { return name; }
public String toString() { return name; }
}
public class EnumEx4 {
public static void main(String[] args) {
MyTransportation t1 = MyTransportation.BUS;
MyTransportation t2 = MyTransportation.BUS;
MyTransportation t3 = MyTransportation.SHIP;
MyTransportation t4 = MyTransportation.AIRPLANE;
MyTransportation t5 = MyTransportation.AIRPLANE;
System.out.printf("t1=%s, %d%n", t1.name(), t1.oridinal());
System.out.printf("t2=%s, %d%n", t2.name(), t2.oridinal());
System.out.printf("t3=%s, %d%n", t3.name(), t3.oridinal());
System.out.printf("t4=%s, %d%n", t4.name(), t4.oridinal());
System.out.printf("t5=%s, %d%n", t5.name(), t5.oridinal());
System.out.println("t1==t2 ? "+ (t1==t2));
System.out.println("t1.compareTo(t3)="+ t1.compareTo(t3));
}
}
// 결과 :
t1=BUS, 0
t2=BUS, 0
t3=SHIP, 2
t4=AIRPLANE, 3
t5=AIRPLANE, 3
t1==t2 ? true
t1.compareTo(t3)=-2