์—ด๊ฑฐ ํƒ€์ž…(enumeration type)๐Ÿ™†โ€โ™‚๏ธ

๊น€๋ฏผ์ œยท2023๋…„ 10์›” 15์ผ
0
post-thumbnail
  • ์—ด๊ฑฐ ํƒ€์ž…์€ ํ•œ์ •๋œ ๊ฐ’์ธ ์—ด๊ฑฐ ์ƒ์ˆ˜ ์ค‘์—์„œ ํ•˜๋‚˜์˜ ์ƒ์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” ํƒ€์ž…์ด๋‹ค.
  • ๋งŒ์•ฝ ์š”์ผ์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ƒ์ˆ˜๋ฅผ ํ‘œํ˜„ํ•˜๊ณ ์žํ•œ๋‹ค๋ฉด ์ด๋ ‡๊ฒŒ ์ฝ”๋“œ๋ฅผ ๊ตฌ์„ฑํ•ด์•ผํ•œ๋‹ค.
    public class DayType {
        public static final int sunday = 0;
        public static final int monday = 1;
        public static final int tuesday = 2;
        public static final int wendesday = 3;
        public static final int thursday = 4;
        public static final int friday = 5;
        public static final int saturday = 6;
    }
    
    public class Exam {
        public static void main(String[] args) {
            int today = DayType.SUNDAY;
            System.out.println(today);
            if (today==DayType.SUNDAY){
                System.out.println("์˜ค๋Š˜์€ ์ผ์š”์ผ!");
            }
        }
    }
    ->0
    	์˜ค๋Š˜์€ ์ผ์š”์ผ!
  • ์ƒ์ˆ˜ ์‚ฌ์šฉ์‹œ์˜ ๋ฌธ์ œ์ 
    • ์ •ํ•ด์ง„ ๊ฐ’๋งŒ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๋ฌธ์ œ์ ์ด ์žˆ๋‹ค
    • ์ด๋Ÿฌํ•œ ๊ฒƒ์„ ํƒ€์ž…์— ์•ˆ์ „ํ•˜์ง€ ์•Š๋‹ค(NoType-Safety)๋ผ๊ณ  ๋งํ•œ๋‹ค.

enum ํƒ€์ž…

  • ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์ด Enum์„ ์ƒ์„ฑํ•œ๋‹ค.
  • enumํƒ€์ž…์€ ์˜๋ฏธ๊ฐ€ ๋ถ€์—ฌ๋œ ์ด๋ฆ„์„ ๊ฐ–๋Š” ์ƒ์ˆ˜์˜ ์„ ์–ธ์„ ๋” ์‰ฝ๊ฒŒ ๋งŒ๋“ค์–ด์ค€๋‹ค.
  • ์œ„์—์„œ ํ•œ ๊ฒƒ๊ณผ ๋˜‘๊ฐ™์€ ์˜ˆ์ œ๋ฅผ enumํƒ€์ž…์œผ๋กœ ๋งŒ๋“ค์–ด๋ณด์ž
    public enum DayType {
        SUNDAY, MONDAY, TUESDAY, WENDESDAY, THURSDAY, FRIDAY, SATURDAY
    }
    • ๋ณดํ†ต ๋ชจ๋‘ ๋Œ€๋ฌธ์ž๋กœ ํ‘œํ˜„ํ•˜๋Š”๋ฐ ์ƒ์ˆ˜์™€ ์ƒ์ˆ˜๋Š” ,๋กœ ๊ตฌ๋ถ„ํ•œ๋‹ค.
  • Enum ํƒ€์ž…์„ ํ•„๋“œ๋กœ ๊ฐ€์ง€๋Š” Today ํด๋ž˜์Šค๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•œ๋‹ค,
  • ํ•œ๋ฒˆ ์ถœ๋ ฅํ•ด๋ณด์ž
    public static void main(String[] args) {
        DayType day = DayType.SUNDAY;
        System.out.println(day);
    }
    ->SUNDAY
  • ๋˜ํ•œ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด ๊ฐ’์„ ์ €์žฅํ•ด์ฃผ๋Š” ํ˜•ํƒœ๋กœ ๋งŒ๋“ค์–ด์ค„ ์ˆ˜๋„ ์žˆ๋‹ค.
    public class DAY {
        private DayType dayType;
    
        public DayType getDayType() {
            return dayType;
        }
    
        public void setDayType(DayType dayType) {
            this.dayType = dayType;
        }
    }
    
    public class Exam {
        public static void main(String[] args) {
            DAY day = new DAY();
            day.setDayType(DayType.SUNDAY);
            System.out.println(day.getDayType());
        }
    }
    ->SUNDAY
    • ์œ„์™€ ๊ฐ™์ด ํด๋ž˜์Šค์— enumํƒ€์ž…์˜ ํ•„๋“œ๋ฅผ ๋งŒ๋“ค๊ณ  setDayType์ด๋ผ๋Š” ๋ฉ”์†Œ๋“œ๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๊ฐ’์„ ๋ฐ›์•„ ํ•„๋“œ์— ์ €์žฅํ•˜๊ณ  getDayType์ด๋ผ๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” ํ˜•ํƒœ๋กœ๋„ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
    • setDayType์—๋Š” enum์ด ๊ฐ€์ง„ ๊ฐ’๋งŒ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.
    • ์ด๋ ‡๊ฒŒ ์ง€์ •ํ•œ ๊ฐ’๋งŒ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์„ ํƒ€์ž…์— ์•ˆ์ „ํ•˜๋‹ค(Type-Safety)๋ผ๊ณ  ํ•œ๋‹ค
  • ๋˜ํ•œ Enum์€ switch๋ฌธ์—์„œ๋„ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค
    public static void main(String[] args) {
        DayType day = DayType.SUNDAY;
    
        switch (day){
            case SUNDAY :
                System.out.println("์ผ์š”์ผ ์ข‹์•„~");
                break;
            default:
                System.out.println("์ผ์š”์ผ์ด ์•„๋‹ˆ์•ผ...");
        }
    }
    ->์ผ์š”์ผ ์ข‹์•„~
    • case ๋‹ค์Œ์—๋Š” Day๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ƒ์ˆ˜๊ฐ€ ๋‚˜์™€์•ผํ•œ๋‹ค.
      • ๋‹ค๋ฅธ๊ฑฐ ๋„ฃ์œผ๋ฉด ์˜ค๋ฅ˜ ๋ฐœ์ƒ!

Enum์€ ์ƒ์„ฑ์ž๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค. ๋‹จ privateํ•ด์•ผํ•จ

  • Enum์€ ์ƒ์„ฑ์ž์˜ ๋‚ด๋ถ€์—์„œ๋งŒ ํ˜ธ์ถœ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
  • Enum์•ˆ์— ์„ ์–ธ๋œ ๋ฉ”์†Œ๋“œ๋‚˜ ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.
  • Object๊ฐ€ ๊ฐ€์ง€๊ณ ์žˆ๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • toString์„ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ•˜๋ฉด ์›ํ•˜๋Š” ์ถœ๋ ฅ๊ฒฐ๊ณผ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.
  • ๊ทธ๋ ‡๋‹ค๋ฉด ์ƒ์„ฑ์ž์™€ toStirng์„ ์‚ฌ์šฉํ•œ ์˜ˆ์ œ๋ฅผ ๋งŒ๋“ค์–ด๋ณด์ž
enum DayType {
    SUNDAY, MONDAY, TUESDAY, WENDESDAY, THURSDAY, FRIDAY, SATURDAY;

    private DayType(){
        System.out.println("1์ฃผ์ฐจ");
    }

    @Override
    public String toString() {
        return "์ผ์š”์ผ์ด ์ข‹๋‹ค..";
    }
}

public class Exam {
    public static void main(String[] args) {
        System.out.println(DayType.SUNDAY);
    }
}
->1์ฃผ์ฐจ
	1์ฃผ์ฐจ
	1์ฃผ์ฐจ
	1์ฃผ์ฐจ
	1์ฃผ์ฐจ
	1์ฃผ์ฐจ
	1์ฃผ์ฐจ
	์ผ์š”์ผ์ด ์ข‹๋‹ค..
  • ์—ด๊ฑฐํ˜• ๊ฐ’์ด 7๊ฐœ ์กด์žฌํ•˜๋ฏ€๋กœ 7๋ฒˆ์˜ ์ƒ์„ฑ์ž ํ˜ธ์ถœ์ด ์ด๋ค„์ง„ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.
  • ๋”ฐ๋ผ์„œ ์—ด๊ฑฐํ˜• ๊ฐ’์€ ์•„๋ž˜์™€๋„ ๊ฐ™๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค.
    public static final DayType SUNDAY = new DayType();
    public static final DayType MONDAY = new DayType();
    ...
    public static final DayType SATURDAY = new DayType();
    • ์ƒ์„ฑ์ž๊ฐ€ private์ด๋ผ ์‹ค์ œ ์ปดํŒŒ์ผ์€ ์•ˆ๋จ
  • ๊ทธ๋ ‡๋‹ค๋ฉด ์ธ์ž๋ฅผ ์ „๋‹ฌ๋ฐ›๋Š” ์—ด๊ฒจํ˜•์˜ ์ƒ์„ฑ์ž๋„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์„๊นŒ
    enum Gender {
        MAN(1),WOMAN(2);
    
        int idNumber;
        private Gender(int idNumber){
            this.idNumber = idNumber;
        }
    
        @Override
        public String toString() {
            return "My idNumber is " + idNumber;
        }
    }
    public class Exam {
        public static void main(String[] args) {
            System.out.println(Gender.MAN);
        }
    }
    ->My idNumber is 1
    • ์ด์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
  • Enum ๊ฐ’์„ ๋น„๊ตํ•  ๋•Œ๋Š” โ€˜==โ€™์„ ์‚ฌ์šฉํ•œ๋‹ค
    • Enum๊ฐ’์€ ์ƒ์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฉ”๋ชจ๋ฆฌ์— ๋”ฑ ํ•œ๊ฐœ๋งŒ ์˜ฌ๋ผ๊ฐ„๋‹ค.

      public static void main(String[] args) {
          DayType dayType1 = DayType.SUNDAY;
          DayType dayType2 = DayType.SUNDAY;
          System.out.println(dayType1 == dayType2);
      }
      ->true

EnumMap

  • Enumํƒ€์ž…์„ ํ‚ค๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ํด๋ž˜์Šค์ด๋‹ค.
public static void main(String[] args) {
    EnumMap emap = new EnumMap(DayType.class);
    emap.put(DayType.SUNDAY, "์ผ์š”์ผ์ด ์ตœ๊ณ ์•ผ ์ง„์งœ.. ์ผ์š”์ผ์€ ์‹ ์ด์•ผ..");
    emap.put(DayType.MONDAY, "์›”์š”์ผ...?์œผใ…กใ…ก์œฝ ์›”์š”์ผ...");
    emap.put(DayType.FRIDAY, "์ผ์ฃผ์ผ ์ค‘ ๊ฐ€์žฅ ์„ค๋ ˆ๋Š” ์š”์ผ? ๊ธˆ์š”์ผ์ด์ง€~");

    System.out.println(emap.get(DayType.SUNDAY));
}
->์ผ์š”์ผ์ด ์ตœ๊ณ ์•ผ ์ง„์งœ.. ์ผ์š”์ผ์€ ์‹ ์ด์•ผ..
  • ์œ„์™€ ๊ฐ™์ด put๋ฉ”์†Œ๋“œ๋กœ (key,value)๊ฐ’์„ ๋„ฃ๊ณ  get๋ฉ”์†Œ๋“œ๋กœ ๊บผ๋‚ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

Enum์€ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ , ํ•ด๋‹น ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•˜์—ฌ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

public interface DAY {
    public void print();
}

enum DayType implements DAY{
    SUNDAY, MONDAY, TUESDAY, WENDESDAY, THURSDAY, FRIDAY, SATURDAY;

    @Override
    public void print() {
        System.out.println("์ธํ„ฐํŽ˜์ด์Šค๋„ ๊ตฌํ˜„๋ฐ›์•„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ!");
    }
}

Enum์€ ์ถ”์ƒ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.

  • ์ถ”์ƒ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐ€์งˆ ๋•Œ์—๋Š” ์ƒ์ˆ˜๋ฅผ ์ •์˜ํ•  ๋•Œ ์ถ”์ƒ ๋ฉ”์†Œ๋“œ๋ฅผ ํ•จ๊ป˜ ๊ตฌํ˜„ํ•ด์ค˜์•ผํ•œ๋‹ค.
enum DayType{
    SUNDAY{
        public void print(){
            System.out.println("์ผ์š”์ผ!!!!");
        }
    }, MONDAY{
        public void print(){
            System.out.println("์›”์š”์ผ....");
        }
    },FRIDAY{
        public void print(){
            System.out.println("๊ธˆ์š”์ผ~~~");
        }
    };
    public abstract void print();
}
  • Enum ๊ฐ์ฒด๋Š” Enum ์ƒ์ˆ˜๊ฐ€ ์ฒ˜์Œ ํ˜ธ์ถœ๋˜๊ฑฐ๋‚˜ ์ฐธ์กฐ๋  ๋•Œ ์ƒ์„ฑ๋œ๋‹ค.
  • Enum์€ Serializable๊ณผ Comparable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ด๋ฏธ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ๋‹ค.
profile
๋ธ”๋กœ๊ทธ ์ด์ „ํ–ˆ์Šต๋‹ˆ๋‹ค!! ๐Ÿ‘‰ https://alswp006.github.io/

0๊ฐœ์˜ ๋Œ“๊ธ€