├── Grub
│ └── Bee
│ ├── HoneyMakerBee
│ └── ForagerBee
이 과정에 맞추어 문제를 해결해보자!
1️⃣ 단계 Grub 풀이 과정
< Grub class functionality >
✓age
속성은0
이어야 합니다
✓color
속성은pink
이어야 합니다
✓food
속성은jelly
이어야 합니다
✓eat
메소드가 존재해야 합니다
✓eat
메소드를 통해Grub
이 젤리를 먹습니다class Grub { constructor() { this.age = 0; // this.color = "pink"; this.food = "jelly"; } eat() { return `Mmmmmmmmm ${this.food}`; } }
👉 this를 생성하여 새로운 속성을 만들 수 있다.
2️⃣ 단계 Bee 풀이과정
< Bee class functionality >
✓age
속성은5
이어야 합니다
✓color
속성은yellow
이어야 합니다
✓food
속성은 Grub으로부터 상속받습니다
✓eat
메소드는 Grub으로부터 상속받습니다
✓job
속성은Keep on growing
이어야 합니다class Bee extends Grub { constructor() { super(); this.age = 5; this.color = "yellow"; this.job = "Keep on growing"; } }
👉 extends와 super를 활용하면 부모 클래스의 함수를 호출할 수 있다.
👉 extends와 super는 this위에서 사용해야 참조오류가 생기지 않는다.
3️⃣ 단계 HoneyMakerBee 풀이과정
< HoneyMakerBee class functionality >
✓age
속성은10
이어야 합니다
✓job
속성은make honey
이어야 합니다
✓color
속성은Bee
로부터 상속받습니다
✓food
속성은Grub
으로부터 상속받습니다
✓eat
메소드는Grub
으로부터 상속받습니다
✓honeyPot
속성은0
이어야 합니다
✓makeHoney
메소드는honeyPot
에 1씩 추가합니다
✓giveHoney
메소드는honeyPot
에 1씩 뺍니다class HoneyMakerBee extends Bee { constructor() { super(); this.age = 10; this.job = "make honey"; this.honeyPot = 0; } makeHoney() { this.honeyPot++; } giveHoney() { this.honeyPot--; }
👉 makeHoney()와 giveHoney()를 실행시킬 때에는 return은 사용하지 않아도 된다.
이 함수 자체가 honeyPot++, honeyPot--을 하기 위해만 존재하기 때문이다.
4️⃣ 단계 ForagerBee 풀이과정
< ForagerBee class functionality >
✓age
속성은10
이어야 합니다
✓job
속성은find pollen
이어야 합니다
✓color
속성은Bee
로부터 상속받습니다
✓food
속성은Grub
으로부터 상속받습니다
✓eat
메소드는Grub
으로부터 상속받습니다
✓canFly
속성은true
이어야 합니다
✓treasureChest
속성은 빈 배열[]
이어야 합니다
✓forage
메소드를 통해treasureChest
속성에 보물을 추가할 수 있어야 합니다class ForagerBee extends Bee { constructor() { super(); this.age = 10; this.job = "find pollen"; this.canFly = true; this.treasureChest = []; } forage(items) { this.treasureChest.push(items); } }
👉 treasureChest 속성에 보물을 추가하는 방법 :
push
메소드 활용하기!