ν°μ€ν 리 λ·°
π μ΄κ±°ν
: μλ‘ κ΄λ ¨λ μμλ₯Ό νΈλ¦¬νκ² μ μΈνκΈ° μν κ²
: μλ°μ μ΄κ±°νμ νμ μ μμ
ππ»βοΈ νμ μ μμ νλ€λ κ²μ μ΄λ€ μλ―ΈμΈκ°μ?
enum Gender { MALE, FEMALE } // MALE: 0, FEMALE: 1
enum PostStatus { OPEN, CLOSE } // OPEN: 0, CLOSE: 1
λ§μ½ μ΄λ κ² νλ©΄ Gender.MALEκ³Ό PostStatus.OPENμ valueκ° λͺ¨λ 0μ΄μ§λ§, νμ μ΄ λ€λ₯΄λ―λ‘ μλ‘ λΉκ΅λ₯Ό ν μ μμ!
if(Gender.MALE == PostStatus.OPEN) // μ»΄νμΌ μλ¬!
: μ΄κ±°ν μμκ° λΉκ΅μμ == μ¬μ© κ°λ₯!
: but >, < λ λΆκ°λ₯! λμ compareTo() μ¬μ©
https://docs.oracle.com/javase/8/docs/api/java/lang/Enum.html
Enum (Java Platform SE 8 )
Returns the enum constant of the specified enum type with the specified name. The name must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.) Note that for a particular enum typ
docs.oracle.com
ππ»βοΈ μ΄κ±°νμ μμμ κ°μ λ°κΎΈκ³ μΆμ΄μ!
π©π»π« μ§μ λ κ°μ μ μ₯ν μ μλ μΈμ€ν΄μ€ λ³μμ μμ±μλ₯Ό μΆκ°ν΄μ£Όλ©΄ λ¨!
enum Gender {
MALE(3), FEMALE(4);
private final int VALUE;
Gender(int value) { VALUE = value; } // 묡μμ μΌλ‘ private μμ±μ!
public int getValue() { return VALUE; } // Gender.MALE.getValue() μ΄λ κ² μ¬μ©!
}
ππ»βοΈ μ΄κ±°νμ λ©μλλ₯Ό μ΄κ±°ν νμ λ§λ€ λ€λ₯΄κ² μ μνκ³ μΆμ΄μ!
π©π»π« μΆμ λ©μλλ‘ κ΅¬ν κ°λ₯!
enum Gender {
MALE(3) { int process(int num) { return value + num } },
FEMALE(4) { int process(int num) { return value - num } };
abstract int process(int num) // numκ³Ό μ±λ³(3, 4)μ κ°μ§κ³ μ²λ¦¬λ₯Ό νλ μΆμ λ©μλ
protected final int VALUE;
Gender(int value) { VALUE = value; }
abstract int getValue(int num) { return value; }
}
βοΈ μ΄λ°κ² λ€ κ°λ₯ν μ΄μ λ μ΄κ±°ν μμ(ex. MALE, FEMALE) νλνλκ° μ΄κ±°νμ (ex. Gender)μ κ°μ²΄μ΄κΈ° λλ¬Έ!
'μΈμ΄ > μλ°' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
μ°λ λ (0) | 2023.07.30 |
---|---|
μ λν μ΄μ (0) | 2023.07.20 |
μ§λ€λ¦μ€ (0) | 2023.07.14 |
컬λ μ νλ μμ (0) | 2023.07.13 |
java.langν¨ν€μ§μ μ μ©ν ν΄λμ€ (0) | 2023.06.29 |