HappyWeasel
Java - enum (열거형 데이터 타입) 본문
아래의 두 코드는 동일한 의미를 가진다.
class Fruit{
public static final Fruit APPLE = new Fruit();
public static final Fruit PEACH = new Fruit();
public static final Fruit BANANA = new Fruit();
}
enum Fruit{
APPLE, PEACH, BANANA
}
APPLE 가져오기.
Fruit f = Fruit.APPLE;
생성자 설정하기
enum Fruit{
APPLE("red"), PEACH("pink"), BANANA("yellow");
private String color;
public String getColor(){
return this.color;
}
Fruit(String color){
System.out.println("Call Constructor " + this);
this.color = color;
}
}
public class Demo{
public static void main(String[] args){
Fruit type = Fruit.APPLE;
switch(type){
case APPLE:
System.out.println(57 + " kcal, color " + Fruit.APPLE.color);
break;
case PEACH:
System.out.println(34 + " kcal, color " + Fruit.PEACH.color);
break;
case BANANA:
System.out.println(93 + " kcal, color " + Fruit.BANANA.color);
break;
}
for ( Fruit f : Fruit.values()){
System.out.println(f);
}
}
}
'Basic > Java' 카테고리의 다른 글
Java - 스택( Stack ), 큐 ( Queue ) (0) | 2019.04.20 |
---|---|
Java - 제너릭 (Generic) (0) | 2019.04.20 |
Java - JVM에 대하여 (스크랩) (0) | 2019.04.20 |
Java - 문자열 클래스 (0) | 2019.04.20 |
Java - 람다식 (Lambda) (0) | 2019.04.20 |
Comments