μμλ λ³νμ§ μλ κ°μ
μλ₯Ό λ€μ΄ λ³νμ§ μλ μμκ°μ λ°λΌμ κ·Έκ°μ ν΄λΉνλ κ³ΌμΌμ μλ―Έλ₯Ό κ³ μ νλ€κ³ κ°μ . κ·Έλ°λ° μ£ΌμμΌλ‘ μμμ μλ―Έλ₯Ό μ λ¬νκ³ μμ§λ§ μ£Όμμ΄ μμ΄μ‘μ κ²½μ°μλ μμλ₯Ό μ¬μ©νλ μ½λλ₯Ό μμ보기 νλ€λ€.
μ΄λ° λμλ μ΄λ¦μ μ μΈν΄ μ£Όλ κ²μ΄ μ’μ λ³μλ₯Ό μ§μ νκ³ κ·Έλ³μλ₯Ό final λ‘ μ²λ¦¬νλ©΄ νλ² μ€μ λ λ³μλ λ°λμ§μμ λν λ°λμ§ μλ κ°μ΄λΌλ©΄ μΈμ€ν΄μ€ λ³μκ°μλλΌ ν΄λμ€ λ³μ static μΌλ‘ μ§μ νλ κ²μ΄ μ’μ
private final static int APPLE=1;
λ€λ₯Έλ΄μ© : switch λ¬Έμ μ¬μ©ν λλ switchμ 쑰건μΌλ‘ μ νλ λ°μ΄ν° νμ
λ§ μ¬μ©κ°λ₯ byte,short,char,int, enum,String,Character,Byte,Short,Integer
μ΄κ±°νμ΄λΌκ³ λΆλ¦(enumerated type)
μ΄κ±°νμ μλ‘ μ°κ΄λ μμλ€μ μ§ν©
enumμ class, interfaceμ λκΈμ νμμ κ°μ§ λ¨μ
μ¬μ€μ classμ΄λ©° νΈμλ₯Ό μν΄μ enumλ§μ μν λ¬Έλ²μ νμμ κ°μ§κ³ μμ, ꡬλΆνκΈ° μν΄ enum ν€μλ μ¬μ©
μ½λκ° λ¨μν΄μ§λ€.
μΈμ€ν΄μ€ μμ±κ³Ό μμμ λ°©μ§
ν€μλ enumμ μ¬μ©νκΈ° λλ¬Έμ ꡬνμ μλκ° μ΄κ±°μμ λΆλͺ
νκ² λνλΌμ μμ
package org .opentutorials .javatutorials .constant2 ;
/*
class Fruit{
public static final Fruit APPLE = new Fruit();
public static final Fruit PEACH = new Fruit();
public static final Fruit BANANA = new Fruit();
}
class Company{
public static final Company GOOGLE = new Company();
public static final Company APPLE = new Company();
public static final Company ORACLE = new COMPANY(Company);
}
*/
// μμ λμΌν μλ―Έ
enum Fruit {
APPLE ,PEACH ,BANANA
}
enum Company {
GOOGLE ,APPLE ,ORACLE
}
public class ConstantDemo {
public static void main (String [] args ) {
/* if(Fruit.APPLE == Company.APPLE){
System.out.println("κ³ΌμΌ μ νκ³Ό νμ¬ μ νμ΄ κ°λ€.");
}
*/
Fruit type = Fruit .APPLE ;
switch (type ){
case APPLE :
System .out .println (57 +" kcal" );
break ;
case PEACH :
System .out .println (34 +" kcal" );
break ;
case BANANA :
System .out .println (93 +" kcal" );
break ;
}
}
}
enum μ μ¬μ€μ ν΄λμ€μ΄κΈ° λλ¬Έμ μμ±μλ₯Ό κ°μ§μ μμ
enum Fruit {
APPLE ("red" ),PEACH ("pink" ),BANANA ("yellow" );
private String color ;
public String getColor (){
return this .color ;
}
Fruit (String color ){
System .out ,println ("Call Constructor" +this );
this .color = color ;
}
}
enum Company {
GOOGLE ,APPLE ,ORACLE
}
public class ConstantDemo {
public static void main (String [] args ) {
Fruit type = Fruit .APPLE ;
switch (type ){
case APPLE :
System .out .println (57 +" kcal" +Fruit .APPLE .getColor ());
break ;
case PEACH :
System .out .println (34 +" kcal" +Fruit .PEACH .getColor ();
break ;
case BANANA :
System .out .println (93 +" kcal" +Fruit .BANANA .getColor ());
break ;
}
for (Fruit f : Fruit .values ()){
System .out .println (f );
}
}
}
μ΄κ±°νμ μ°κ΄λ κ°λ€μ μ μ₯ν¨
λν κ·Έκ°λ€μ΄ λ³κ²½λμ§ μλλ‘ λ³΄μ₯ν¨
μ΄κ±°ν μμ²΄κ° ν΄λμ€μ΄κΈ° λλ¬Έμ μ΄κ±°ν λ΄λΆμ μμ±μ,νλ λ©μλλ₯Ό κ°μ§μκ° μμ΄μ λ¨μν μμκ° μλλΌ λλ§μ μν μ ν μ μμ
enumμ μ¬μ©ν΄μ switch/case λ¬Έ λμ ꡬννκΈ° μ
enum Expression {
PLUS ("+" ), MINUS ("-" ), TIMES ("*" ), DIVIDE ("/" );
private String expression
Expression (String expression ){
this .expression = expression ;
}
static Expression of (String expression ){
return Arrays .stream (values ())
.filter (v -> expression .equals (v .expression ))
.findFirst ()
.orElseThrow (() -> new IllegealArgumentException (String .format ("%s μ¬μΉμ°μ°μ ν΄λΉλμ§ μμ ννμμ
λλ€." ,expression )));
}
}
enumμ μ¬μ©ν΄μ if/else λ¬Έ λμ ꡬννκΈ° μμ
μλμ κ°μ΄ ꡬννλ©΄ λΆνμν if/else λ¬Έμ μλ΅ν μ μμ
Java Enum νμ©κΈ° λ§ν¬μμ ν΄λΉ μ½λ λ°μ·
public enum PayGroup {
CASH("νκΈ", Arrays.asList("ACCOUNT_TRANSFER", "TOSS")),
CARD("μΉ΄λ", Arrays.asList("CARD", "KAKAO_PAY", "PAYCO")),
ETC("κΈ°ν", Arrays.asList("POINT", "COUPON")),
EMPTY("μμ", Collections.EMPTY_LIST);
private String title;
private List<String> payList;
PayGroup(String title, List<String> payList) {
this.title = title;
this.payList = payList;
}
public static PayGroup findByPayCode(String code) {
return Arrays.stream(values())
.filter(payGroup -> payGroup.hasPayCode(code))
.findAny()
.orElse(EMPTY);
}
private boolean hasPayCode(String code) {
return payList.stream()
.anyMatch(pay -> pay.equals(code));
}
public String getTitle() {
return title;
}
}