๊ด€๋ฆฌ ๋ฉ”๋‰ด

ejyoo's ๊ฐœ๋ฐœ ๋…ธํŠธ

Enum(์—ด๊ฑฐํ˜•) ๋ณธ๋ฌธ

BackEnd/Java

Enum(์—ด๊ฑฐํ˜•)

ejyoovV 2021. 3. 8. 20:40

๐Ÿ’ก Enum ์ด๋ž€?

enum type์€ ์ƒ์ˆ˜(Constant)๋กœ ์‚ฌ์šฉํ•  ๊ฐ’๋“ค์„ ๋ฏธ๋ฆฌ ์„ ์–ธํ•˜์—ฌ ์‚ฌ์šฉํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ํŠน๋ณ„ํ•œ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด๋‹ค.

 

๐Ÿ’ก Enum ์‚ฌ์šฉ ์‹œ ์žฅ์ 

  • ๊ธฐ์กด static final ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•œ ์ƒ์ˆ˜ ์„ ์–ธ ๋ฐฉ์‹์—์„œ๋Š” ํ•ด๋‹น ์ƒ์ˆ˜์˜ ๊ฐ’๋งŒ ๋น„๊ตํ•˜๊ฒŒ ๋˜๋Š” ๋ฐ˜๋ฉด, enum์„ ์ด์šฉํ•˜์—ฌ ์ƒ์ˆ˜๋ฅผ ์ •์˜ํ•˜๊ฒŒ ๋˜๋ฉด ๊ฐ’ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ํƒ€์ž…๊นŒ์ง€๋„ ์ฒดํฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํƒ€์ž…์˜ ์•ˆ์ „ํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜์žˆ๋‹ค.
  • ๊ธฐ์กด ์ƒ์ˆ˜ ์„ ์–ธ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋Š” ์ฝ”๋“œ์—์„œ๋Š” ์ƒ์ˆ˜๊ฐ’์ด ๋ณ€๊ฒฝ๋˜๋Š” ๊ฒฝ์šฐ ํ•ด๋‹น ์ƒ์ˆ˜๋ฅผ ์ฐธ์กฐํ•˜๋Š” ์†Œ์Šค์ฝ”๋“œ๋„ ๋ชจ๋‘ ์žฌ์ปดํŒŒ์ผ ํ•ด์ค˜์•ผ ํ•˜๋Š”๋ฐ enum ์ƒ์ˆ˜๋ฅผ ์ •์˜ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ๊ทธ๋Ÿด ํ•„์š”๊ฐ€ ์—†๋‹ค.

๐Ÿ’ก ๊ธฐ์กด ์ƒ์ˆ˜ ์„ ์–ธ ๋ฐฉ๋ฒ•๊ณผ enum ์ƒ์ˆ˜ ์„ ์–ธ ๋ฐฉ๋ฒ• ๋น„๊ต

//์—ด๊ฑฐํ˜• => ์ƒ์ˆ˜๊ฐ’๋“ค์„ ์„ ์–ธํ•˜๋Š” ๋ฐฉ๋ฒ•
static final int A = 0;
static final int B = 1;
static final int C = 2;
static final int D = 3;
	 	
enum Data {A, B, C, D};

//์—ด๊ฑฐํ˜• ์„ ์–ธํ•˜๋Š” ๋ฐฉ๋ฒ•
enum ์—ด๊ฑฐํ˜• ์ด๋ฆ„ {์ƒ์ˆ˜๊ฐ’1, ์ƒ์ˆ˜๊ฐ’2, ์ƒ์ˆ˜๊ฐ’3, ..., ์ƒ์ˆ˜๊ฐ’n}

๐Ÿ’ก ์—ด๊ฑฐํ˜•์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์„œ๋“œ

1) name() => ์—ด๊ฑฐํ˜• ์ƒ์ˆ˜์˜ ์ด๋ฆ„์„ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

2) ordinal() => ์—ด๊ฑฐํ˜• ์ƒ์ˆ˜๊ฐ€ ์ •์˜๋œ ์ˆœ์„œ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. (๊ธฐ๋ณธ์ ์œผ๋กœ 0๋ถ€ํ„ฐ ์‹œ์ž‘)

3) valueOf("์—ด๊ฑฐํ˜• ์ƒ์ˆ˜์ด๋ฆ„"); => ์ง€์ •๋œ ์—ด๊ฑฐํ˜•์—์„œ '์—ด๊ฑฐํ˜•์ƒ์ˆ˜์ด๋ฆ„'๊ณผ ์ผ์น˜ํ•˜๋Š” ์—ด๊ฑฐํ˜• ์ƒ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

๐Ÿ’ก City๋ผ๋Š” ์—ด๊ฑฐํ˜• ๊ฐ์ฒด ์‚ฌ์šฉ(๊ธฐ๋ณธ๊ฐ’์„ ์ด์šฉํ•˜๋Š” ์—ด๊ฑฐํ˜•)

- name() ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์ˆ˜ ์ถœ๋ ฅ๊ณผ

- ordinal() ์„ ์‚ฌ์šฉํ•˜์—ฌ ์—ด๊ฑฐํ˜• ์ƒ์ˆ˜ ์œ„์น˜๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜์—ฌ  ์ถœ๋ ฅํ•œ๋‹ค.

public class Main {
	public enum City { ์„œ์šธ, ๋ถ€์‚ฐ, ๋Œ€๊ตฌ, ๊ด‘์ฃผ, ๋Œ€์ „ };
	
	public static void main(String[] args) {
		City myCity1;//์—ด๊ฑฐํ˜• ์ƒ์ˆ˜ ๊ฐ์ฒด ๋ณ€์ˆ˜ ์„ ์–ธ
		City myCity2;
		
//		City enum์—์„œ '์„œ์šธ' ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.
		myCity1 = City.valueOf("์„œ์šธ");
//		์—ด๊ฑฐํ˜• ๊ฐ์ฒด ๋ณ€์ˆ˜์— ๊ฐ’ ์ €์žฅํ•˜๊ธฐ
		myCity2 = City.๋ถ€์‚ฐ;
		
		
		System.out.println("myCity1 : " + myCity1.name());
		System.out.println("myCity1์˜ oardinal : " + myCity1.ordinal());
		System.out.println();
		
		System.out.println("myCity2 : " + myCity2.name());
		System.out.println("myCity2์˜ oardinal : " + myCity2.ordinal());
		System.out.println();
	}
}

๐Ÿ’ก ์—ด๊ฑฐํ˜• ๊ฐ์ฒด ๊ฐ„ ๋น„๊ต

public class Main {
	public enum City { ์„œ์šธ, ๋ถ€์‚ฐ, ๋Œ€๊ตฌ, ๊ด‘์ฃผ, ๋Œ€์ „ };
	
	public static void main(String[] args) {
		City city = City.๋Œ€๊ตฌ;
		
		System.out.println("๋Œ€๊ตฌ์™€ ๋Œ€์ „์ด ๊ฐ™์€๊ฐ€? true/false : " + (city == City.๋Œ€์ „));
		System.out.println("๋Œ€๊ตฌ์™€ ๋Œ€๊ตฌ๊ฐ€ ๊ฐ™์€๊ฐ€? true/false : " + (city == City.๋Œ€๊ตฌ));
		
		System.out.println("๋Œ€๊ตฌ์™€ ๋Œ€๊ตฌ ๋น„๊ต => " + city.compareTo(City.๋Œ€๊ตฌ));
		System.out.println("๋Œ€๊ตฌ์™€ ์„œ์šธ ๋น„๊ต => " + city.compareTo(City.์„œ์šธ));
		System.out.println("๋Œ€๊ตฌ์™€ ๋Œ€์ „ ๋น„๊ต => " + city.compareTo(City.๋Œ€์ „));
	}
}

 

 

๐Ÿ’ก ๋ฐ์ดํ„ฐ ๊ฐ’์„ ์ž„์˜๋กœ ์ง€์ •ํ•œ ์—ด๊ฑฐํ˜• Season ๊ฐ์ฒด ์„ ์–ธ

๋ฐ์ดํ„ฐ ๊ฐ’์„ ์ •ํ•ด์ค„ ๊ฒฝ์šฐ์—๋Š” ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค์–ด์„œ ๊ด„ํ˜ธ์†์˜ ๊ฐ’์ด ๋ณ€์ˆ˜์— ์ €์žฅ๋˜๋„๋ก ํ•œ๋‹ค.

- valueOf ๋ฅผ ์จ์„œ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•๊ณผ ์ง์ ‘ ์—ด๊ฑฐํ˜• ์ƒ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ’์„ ์ถ”์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

public class Main {
	public enum Season {
		๋ด„("3์›”๋ถ€ํ„ฐ 5์›”๊นŒ์ง€"),
		์—ฌ๋ฆ„("6์›”๋ถ€ํ„ฐ 8์›”๊นŒ์ง€"),
		๊ฐ€์„("9์›”๋ถ€ํ„ฐ 11์›”๊นŒ์ง€"),
		๊ฒจ์šธ("12์›”๋ถ€ํ„ฐ 2์›”๊นŒ์ง€");
		
//		๊ด„ํ˜ธ์†์˜ ๊ฐ’์ด ์ €์žฅ๋  ๋ณ€์ˆ˜ ์„ ์–ธ
		private String str;
		
//		์ƒ์„ฑ์ž ๋งŒ๋“ค๊ธฐ(์—ด๊ฑฐํ˜•์˜ ์ƒ์„ฑ์ž๋Š” ์ œ์–ด์ž๊ฐ€ ๋ฌต์‹œ์ ์œผ๋กœ 'private'์ด๋‹ค.)
		Season(String data){//private Season(String data){ ์™€ ๊ฐ™๋‹ค.
			str = data;
		}
		
//		๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ ์ž‘์„ฑ
		public String getStr() {
			return str;
		}
	}
	
	public static void main(String[] args) {
		Season ss1 = Season.valueOf("์—ฌ๋ฆ„");
		Season ss2 = Season.๊ฐ€์„;
		
		System.out.println("Season ss1 is name() : " + ss1.name());
		System.out.println("Season ss1 is ordinal() : " + ss1.ordinal());
		System.out.println("Season ss1 is get() : " + ss1.getStr());
		System.out.println("โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– ");
		System.out.println("Season ss2 is name() : " + ss2.name());
		System.out.println("Season ss2 is ordinal() : " + ss2.ordinal());
		System.out.println("Season ss2 is get() : " + ss2.getStr());
		
	}
}

 

๐Ÿ’ก ์—ด๊ฑฐํ˜• Season ๊ฐ์ฒด ๋‚ด ์—ด๊ฑฐํ˜• ์ƒ์ˆ˜ ๋ฐ์ดํ„ฐ๋“ค์„ ๋ฐฐ์—ด๋กœ ๊ฐ€์ ธ์™€ ์ถœ๋ ฅ

public class Main {
	public enum Season {
		๋ด„("3์›”๋ถ€ํ„ฐ 5์›”๊นŒ์ง€"),
		์—ฌ๋ฆ„("6์›”๋ถ€ํ„ฐ 8์›”๊นŒ์ง€"),
		๊ฐ€์„("9์›”๋ถ€ํ„ฐ 11์›”๊นŒ์ง€"),
		๊ฒจ์šธ("12์›”๋ถ€ํ„ฐ 2์›”๊นŒ์ง€");
		
//		๊ด„ํ˜ธ์†์˜ ๊ฐ’์ด ์ €์žฅ๋  ๋ณ€์ˆ˜ ์„ ์–ธ
		private String str;
		
//		์ƒ์„ฑ์ž ๋งŒ๋“ค๊ธฐ(์—ด๊ฑฐํ˜•์˜ ์ƒ์„ฑ์ž๋Š” ์ œ์–ด์ž๊ฐ€ ๋ฌต์‹œ์ ์œผ๋กœ 'private'์ด๋‹ค.)
		Season(String data){//private Season(String data){ ์™€ ๊ฐ™๋‹ค.
			str = data;
		}
		
//		๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ ์ž‘์„ฑ
		public String getStr() {
			return str;
		}
	}
	
	public static void main(String[] args) {
		Season[] enumArr = Season.values();
		
		for(int i = 0 ; i < enumArr.length ; i++) {
			System.out.println(enumArr[i].name() + " : " + enumArr[i].getStr());
		}
		System.out.println();
	}
}

'BackEnd > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

MAC / ORACLE / DOCKER / OJDBC / Eclipse ์—ฐ๋™  (0) 2021.03.14
์ œ๋„ˆ๋ฆญ ์ดํ•ด  (0) 2021.03.09
Properties  (0) 2021.03.08
Map  (0) 2021.03.08
Hash - equals()์™€ hash() ๋ฉ”์„œ๋“œ ๊ตฌํ˜„  (0) 2021.03.08