열거형
-
자바 - 열거형(enums)언어/JAVA 2023. 3. 26. 16:51
1. 열거형이란? - 열거형은 서로 관련된 상수를 묶어놓은 것이다. java의 열거형은 타입도 관리하여 타입에 안전한 열거형이다. - 마치 인스턴스가 미리 정의된 클래스로 생각해 볼 수 있다. 1.2 열거형의 정의와 사용 - enum 열거체명 {상수명1,상수명2..} 등으로 정의할 수 있다. - 선언 위치는 클래스와 동일하다. 클래스 내부, 외부, 혹은 하나의 클래스파일에 정의할 수 있다. - 사용법은 열거체 참조변수에 열거체.상수명 혹은 열거형체.valueOf(상수명) - 모든 열거형의 조상 Enum에 Enum에 기본적으로 사용가능한 메서드들이 정의되어 있다. enum Direction {EAST,SOUTH,WEST,NORTH} Direction dir = Direction.EAST; Direction..