250x250
Notice
Recent Posts
Recent Comments
관리 메뉴

탁월함은 어떻게 나오는가?

[JAVA] enum: 열거형 함수 본문

[Snow-ball]프로그래밍(컴퓨터)/java

[JAVA] enum: 열거형 함수

Snow-ball 2021. 3. 5. 12:25
반응형

 

 

 

 

 

 

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.example.demo.javaClass.fourthClass;
 
// enum: 열거형
// 열거형은 내가 선언한것들만 제외하고 사용하지못하게한다.
enum Direction { EAST, SOUTH, WEST, NORTH }
 
class EnumEx1 {
    public static void main(String[] args) {
        Direction d1 = Direction.EAST;
        // valueof: 입력받은 인자 값을 지정된 Number객체 형으로 변환하여 반환.
        // 인자값은 primative data type (원시 데이터형 : byte, int, long, double,
        // boolean 등 자바 키워드로 이미 지정된 데이터 형 ) 또는 문자열 등이 될 수 있다.
        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));
        System.out.println("d1 == d3 ? " + (d1 == d3));
        System.out.println("d1.equals(d3) ? " + d1.equals(d3));
        //System.out.println("d2 > d3 ? " + d1 > d3); // Error
 
        // d1과 d3가 같으므로 0을 반환
        System.out.println("d1.compareTo(d3) ? " + (d1.compareTo(d3)));
        // d1과 d2가 다르므므로 -2(음수)을 반환로
        System.out.println("d1.compareTo(d2) ? " + (d1.compareTo(d2)));
 
        switch (d1) {
            case EAST: //Directuin,EAST라고 쓸 수 없다.
                System.out.println("The direction is EAST.");
                break;
            case SOUTH:
                System.out.println("The direction is SOUTH.");
                break;
            case WEST:
                System.out.println("The direction is WEST.");
            case NORTH:
                System.out.println("The direction is NPRTH.");
                break;
            default:
                System.out.println("Invalid direction.");
                break;
        }
 
        Direction[] dArr = Direction.values();
 
        for (Direction d : dArr) // for(Direction d : Direction.valuse())
            System.out.printf("%s=%d%n", d.name(), d.ordinal());
    }
}
 
cs
반응형
Comments