enum

알파로그·2023년 3월 12일
0

JAVA 문법과 지식

목록 보기
1/9

✏️ enum (열거형) 이란

서로 연관된 상수들의 집합

✏️ enum class 를 사용할경우

  • enum 안에서 선언된 상수들은 값이 변경되지 않도록 보장 받는다.
  • 상수를 각각 instance 화 하지 않아도 되서 리팩토링에 효과적이다.
  • 참조 data type 도 기본 data type 만 요구하는 파라미터에 들어갈 수 있다.
  • class 사용법과 완전히 동일하다 (생성자, getter, setter method 사용가능)
  • for 문과 values() method 로 enum 안의 상수들을 열거할 수 있다. (배열처럼 사용가능)

✏️ 1. enum 을 사용하지 않았을 때의 문제

class Emotion {		// 참조 data type 생성
	public static Emotion h = new Emotion();
	public static Emotion s = new Emotion();
	public static Emotion a = new Emotion();
}
class Alphabet {		// 참조 data type 생성
	public static Alphabet h = new Alphabet();
	public static Alphabet s = new Alphabet();
	public static Alphabet a = new Alphabet();
}
public class Prectice {
	public static void start () {
		// 참조 data type 으로 변수 생성
		Emotion input = Emotion.h;
		
		//	switch 는 원시 data type 만 수용가능
		//	*** 참조 데이터 타입이 들어와서 오류가 발생 ***
		switch(input) {
			case Emotion.h :
				System.out.println("^_^");
				break;
			case Emotion.s :
				System.out.println("ㅜ_ㅜ");
				break;
			case Emotion.a :
				System.out.println("-_-");
				break;
		}
	}

예제와 같이 Switch 문에 참조 data type 이 매개변수로 들어와서 오류가 발생됨

✏️ 2.해결방법

enum Emotion{	//enum class 생성
	h,s,a
}
enum Alphabet{
	h,s,a
}
public class Prectice {
	public static void start () {
		Emotion input = Emotion.h;
		
		// data type 이 enum 으로 설정되어 오류가 해결
		switch(input) {
		// input 변수에서 이미 Emotion을 명시했기 때문에
			case h :	//따로 class 를 적어놓지 않는다
				System.out.println("^_^");
				break;
			case s :
				System.out.println("ㅜ_ㅜ");
				break;
			case a :
				System.out.println("-_-");
				break;
		}
	}

✏️ 3.enum의 생성자 와 method

enum Emotion{
		//상수 선언
	h("Happy",100),s("Sad",50),a("Angrry",10);
		//맴버 선언
	private String full_name;
	private int grade;
		// 생성자 선언
	Emotion (String full_name, int grade){
		this.full_name = full_name;
		this.grade = grade;
	}
		// getter method  생성
	public String getFull_name() {
		return this.full_name;
	}
	public int getGrade() {
		return this.grade;
	}
}
public class Prectice {
	public static void start () {
		Emotion input = Emotion.h;
		
		switch(input) {
			case h :	//따로 class 를 적어놓지 않아도 
				System.out.println("^_^ = "
						+ Emotion.h.getFull_name()+ " "
						+Emotion.h.getGrade());
				break;
			case s :
				System.out.println("ㅜ_ㅜ = "
						+ Emotion.s.getFull_name() 
						+Emotion.s.getGrade());
				break;
			case a :
				System.out.println("-_- = "
						+ Emotion.a.getFull_name() 
						+Emotion.a.getGrade());
				break;
		}
	}

결과값 : ^-^ = Happy 100

✏️ 4.enum 내 상수 열거 하기 (배열화)

enum Emotion{
	h("Happy",100),s("Sad",50),a("Angrry",10);
	private String full_name;
	private int grade;
	Emotion (String full_name, int grade){
		this.full_name = full_name;
		this.grade = grade;
	}
	public String getFull_name() {
		return this.full_name;
	}
	public int getGrade() {
		return this.grade;
	}
}
public class Prectice {
	public static void start () {
		  //enum 배열화
		for(Emotion e : Emotion.values()) {
			System.out.println(e+" = "+e.getFull_name()+" "+e.getGrade());
		}
		
	}
  

결과값 :
h = Happy 100
s = Sad 50
a = Angrry 10

🔗 참고 : 생활코딩 java - 상수와 enum

profile
잘못된 내용 PR 환영

0개의 댓글