[Java] Enum ํ์
๐ ๋ค์ด๊ฐ๋ฉฐ
์ํ ์ฝ๋๋ ๋ถ๋ฅ์ฒ๋ผ ์ ํด์ง ๊ฐ์ ์งํฉ์ ๋ค๋ฃฐ ๋, ์์ ์๋ final ์์๋ฅผ ๋์ดํด์ ์ผ๋ค. ํ์ง๋ง ์ฌ๋ฌ ํด๋์ค์์ ์ฐ๊ธฐ ๋ฒ๊ฑฐ๋กญ๊ณ , ๊ด๋ จ๋ ๊ฐ๋ค์ ํ๋ฐ ๋ฌถ์ด ๊ด๋ฆฌํ๊ธฐ ์ด๋ ค์ ๋ค. ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ๊ฒ์ด Enum์ด๋ค.
Enum์ด๋? โEnumeration(์ด๊ฑฐ)โ์ ์ฝ์. ๋ก์ง๋ณด๋ค๋ ๋ฐ์ดํฐ๋ฅผ ์ด๊ฑฐํ ์์๋ค์ ์งํฉ์ด๋ค. ์๋ฐ์์ ์์ ์ฒ๋ฆฌ๋ฅผ ๋๋งก๋๋ค.
1. ๊ณผ๊ฑฐ์ ์์ ์ฒ๋ฆฌ
์์ ์๋ final ์ ์ด์๋ก ์์๋ฅผ ๋ง๋ค์๋ค.
1
2
3
4
5
6
7
class PastFinal {
private final static int ONE = 1;
public static void main(String[] args) {
System.out.println(PastFinal.ONE);
}
}
โ ๏ธ ์ด ๋ฐฉ์์ ์ฌ๋ฌ ํด๋์ค์์ ์ฐ๊ธฐ ๋ฒ๊ฑฐ๋กญ๊ณ , ๊ด๋ จ ์์๋ค์ ์บก์ํํ ์ ์๋ค. โ์ด ์์๋ค์ ํ๋๋ก ๋ฌถ์ ์ ์์๊น?โ์์ ๋์จ ๊ฒ์ด Enum์ด๋ค.
2. Enum ์ ์ธ
๊ฐ ์์์ ์ฝ๋์ ์ค๋ช ์ ํจ๊ป ๋งคํํ๋ Enum์ ๋ง๋ค์ด ๋ณด์.
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
// OutputType.java
public enum OutputType {
INSPEC ("01", "์ ๊ฒ ์ค"),
SUCCESS("02", "์ ์ ์ข
๋ฃ"),
FAIL ("03", "์ฒ๋ฆฌ ์ค๋ฅ"),
SB ("04", "๋ฐ์ก"),
HW ("05", "์์์
(์ฌํ ๋น)"),
UNKNOWN("", "unknown");
OutputType(String code, String description) { // ์์ฑ์
this.code = code;
this.descriptions = description;
}
private final String code;
private final String descriptions;
public String getCode() { return code; }
public String getDescriptions() { return descriptions; }
// ์ฝ๋๋ก Enum ๊ฐ์ฒด๋ฅผ ์ฐพ๋ ๋ฉ์๋
public static OutputType getByCode(String code) {
for (OutputType type : OutputType.values()) {
if (type.getCode().equalsIgnoreCase(code)) {
return type;
}
}
return OutputType.UNKNOWN;
}
}
๊ตฌ์กฐ๋ฅผ ํ๋์ฉ ๋ฏ์ด๋ณด๋ฉด ์ด๋ ๋ค.
| ๊ตฌ์ฑ ์์ | ์ญํ |
|---|---|
INSPEC("01","์ ๊ฒ ์ค") | ๊ฐ Enum ๊ฐ์ฒด ์ ์ธ (์์ฑ์ ์ธ์ = ์ฝ๋, ์ค๋ช ) |
private final String code, descriptions | ๊ฐ ๊ฐ์ฒด๊ฐ ๊ฐ์ง ํ๋ |
์์ฑ์ OutputType(...) | ์ฝ๋ยท์ค๋ช ์ ํ๋์ ๋งคํ |
getCode() / getDescriptions() | ๋งคํ๋ ๊ฐ์ ๊บผ๋ด๋ Getter |
getByCode(code) | ์ฝ๋๋ก ํด๋น Enum ๊ฐ์ฒด๋ฅผ ์ฐพ์ ๋ฐํ |
๐ก
values()๋ Enum์ ๊ธฐ๋ณธ ์ ๊ณต๋๋ ๋ฉ์๋๋ก, ๊ทธ Enum์ ๋ชจ๋ ๊ฐ์ฒด๋ฅผ ๋ฐฐ์ด๋ก ๋๋ ค์ค๋ค.getByCode()๋ ์ด๊ฑธ ์ํํ๋ฉฐgetCode()๋ก ๋น๊ตํด ํ๋์ ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ค.
3. Enum ์ฌ์ฉ
์ฌ์ฉ์ ์์ฃผ ์ฝ๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
// ์ฝ๋ ์ถ๋ ฅ
System.out.println(OutputType.INSPEC.getCode());
// โ 01
// ์ค๋ช
์ถ๋ ฅ
System.out.println(OutputType.INSPEC.getDescriptions());
// โ ์ ๊ฒ ์ค
// ์ฝ๋๋ก ๊ฐ์ฒด๋ฅผ ์ฐพ์ ์ค๋ช
์ถ๋ ฅ
OutputType findType = OutputType.getByCode("03");
System.out.println(findType.getDescriptions());
// โ ์ฒ๋ฆฌ ์ค๋ฅ
| ์์ฒญ | ์ฝ๋ | ๊ฒฐ๊ณผ |
|---|---|---|
| INSPEC์ ์ฝ๋ | OutputType.INSPEC.getCode() | 01 |
| INSPEC์ ์ค๋ช | OutputType.INSPEC.getDescriptions() | ์ ๊ฒ ์ค |
| ์ฝ๋ 03์ ์ค๋ช | getByCode("03").getDescriptions() | ์ฒ๋ฆฌ ์ค๋ฅ |
๐ ์ ๋ฆฌ
1
2
3
4
5
Enum
โโ ๊ฐ๋
์ ํด์ง ๊ฐ์ ์งํฉ์ ์บก์ํํ ์์ ํ์
โโ ์ ์ธ ๊ฐ์ฒด๋ง๋ค ์ฝ๋ยท์ค๋ช
๋ฑ ํ๋๋ฅผ ์์ฑ์๋ก ๋งคํ
โโ ๊ธฐ๋ณธ values()๋ก ์ ์ฒด ๊ฐ์ฒด ์ํ ๊ฐ๋ฅ
โโ ํ์ฉ getByCode() ๊ฐ์ ์ ์ ๋ฉ์๋๋ก ์ฝ๋โ๊ฐ์ฒด ๋ณํ
| ๊ฐ๋ | ํ ์ค ์ ์ |
|---|---|
| Enum | ๊ด๋ จ ์์๋ฅผ ํ๋ยท๋ฉ์๋์ ํจ๊ป ๋ฌถ์ ํ์ |
| values() | Enum์ ๋ชจ๋ ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ ๊ธฐ๋ณธ ๋ฉ์๋ |
| getByCode ํจํด | ์ธ๋ถ ์ฝ๋๊ฐ์ Enum ๊ฐ์ฒด๋ก ๋งคํ |
Enum์ ๋จ์ํ ์์ ๋์ด์ ๋์ด, ๊ฐ๊ณผ ๊ทธ์ ๋ธ๋ฆฐ ๋ฐ์ดํฐยท๋์์ ํ๋๋ก ์บก์ํํ๋ค. ์ํ ์ฝ๋ ๊ด๋ฆฌ์ฒ๋ผ โ์ ํด์ง ๊ฐ์ ์งํฉโ์ ๋ค๋ฃฐ ๋ final ์์๋ณด๋ค ํจ์ฌ ์์ ํ๊ณ ํํ๋ ฅ์ด ์ข๋ค.
Comments powered by Disqus.