-
[Javascript] ์์, extends, super, overriding,์ค๋ฒ๋ผ์ด๋ฉ์น/JavaScript 2023. 1. 11. 13:18
๐ก ์์ inheritance
์๋ก ๋ค๋ฅธ ํด๋์ค๋ ์์ฑ์ ํจ์๊ฐ ๊ฐ์ ์์ฑ๋ค์ ๊ณต์ ํ ๋
์ด๋ค์ ๊ด๊ณ๋ฅผ ์ ์ํจ์ผ๋ก์จ ์ฝ๋์ ์ค๋ณต์ ์ค์ด๊ณ ํจ์จ์ ๋์- "Bํด๋์ค๋ Aํด๋์ค์์ ํ์๋๋ค." - "B๋ A์ ํ์๋ถ๋ฅ"
I. ํด๋์ค์ ์์ ๋ฌธ๋ฒ
class Bird {wings = 2;}class Eagle extends Bird {claws = 2;}class Penguin extends Bird {swim () { console.log('์์์ค...'); }}class EmperorPenguin extends Penguin {size = 'XXXL';}const birdy = new Bird();const eaglee = new Eagle();const pengu = new Penguin();const pengdol = new EmperorPenguin();console.log(birdy, eaglee, pengu, pengdol);for (const i of [[ '1.', birdy instanceof Bird ],[ '2.', eaglee instanceof Bird ],[ '3.', eaglee instanceof Eagle ],[ '4.', pengdol instanceof Penguin ],[ '5.', pengdol instanceof Bird ],[ '6.', birdy instanceof Eagle ]]) {console.log(i[0], i[1]);}pengu.swim();pengdol.swim();eaglee.swim();- ํด๋์ค์์๋ extends (๋ถ๋ชจํด๋์ค)๋ก ์์๊ด๊ณ ์ ์
- ์์ ํด๋์ค์์ ๋ ๋ค๋ฅธ ํด๋์ค๊ฐ ์์๋ฐ์ ์ ์์
- ์์ ํด๋์ค๋ ๋ถ๋ชจ ํด๋์ค์ ์์ฑ์ ๊ธฐ๋ณธ์ ์ผ๋ก ๊ฐ์ ธ์ด
- ์์ ํด๋์ค์ ์ธ์คํด์ค๋ ๋ถ๋ชจ ํด๋์ค์ ์ธ์คํด์ค๋ก ์ธ์๋จ
- [[Protorype]]์ผ๋ก ์์๊ด๊ณ ์ดํด๋ณผ ๊ฒ - โญ๏ธ ์ต์ข ์ ์ผ๋ก Object
class YalcoChicken {no = 0;menu = { 'ํ๋ผ์ด๋': 10000, '์๋ ์นํจ': 12000 };โconstructor (name, no) {this.name = name;if (no) this.no = no;}introduce () {return `์๋ ํ์ธ์, ${this.no}ํธ ${this.name}์ ์ ๋๋ค!`;}order (name) {return `${this.menu[name]}์์ ๋๋ค.`}}class YalcoChickenAndCafe extends YalcoChicken {cafeMenu = { '์๋ฉ๋ฆฌ์นด๋ ธ': 4000, '๋ผ๋ผ': 4500 };cafeOrder (name) {return `${this.cafeMenu[name]}์์ ๋๋ค.`}}โconst chain1 = new YalcoChickenAndCafe('์๋ฉด', 2)console.log(chain1);console.log(chain1.order('ํ๋ผ์ด๋'),chain1.cafeOrder('๋ผ๋ผ'));
II. ์ค๋ฒ๋ผ์ด๋ฉ overriding
์์ ํด๋์ค์์ ๋ถ๋ชจ๋ก๋ถํฐ ๋ฌผ๋ ค๋ฐ์ ์์ฑ์ด๋ ๊ธฐ๋ฅ์ ๋ฎ์ด์
class Bird {wings = 2;canFly = true;travel () { console.log('๋นํ์ค...') }}class Eagle extends Bird {claws = 2;}class Penguin extends Bird {canFly = false;travel () { console.log('์์์ค...') }}const eaglee = new Eagle();const pengu = new Penguin();console.log(eaglee);eaglee.travel();console.log(pengu);pengu.travel();class YalcoChicken {no = 0;menu = { 'ํ๋ผ์ด๋': 10000, '์๋ ์นํจ': 12000 };โconstructor (name, no) {this.name = name;if (no) this.no = no;}introduce () {return `์๋ ํ์ธ์, ${this.no}ํธ ${this.name}์ ์ ๋๋ค!`;}order (name) {return `${this.menu[name]}์์ ๋๋ค.`}}class YorkNannyYalcoChicken extends YalcoChicken {introduce () {return `๋ ๋ญ ์ณ๋จน์ผ๋ฌ ๊ธฐ์ด๋ค์ด์์ด.`;}order (name) {return `${this.menu[name]}์์ด์ฌ.`}}โconst chain1 = new YorkNannyYalcoChicken ('์ข ๋ก', 48);console.log(chain1.introduce());console.log(chain1.order('์๋ ์นํจ'));
III. super
๋ถ๋ชจ ํด๋์ค์ constructor ๋๋ ๋ฉ์๋ ํธ์ถ
class YalcoChicken {no = 0;menu = { 'ํ๋ผ์ด๋': 10000, '์๋ ์นํจ': 12000 };โconstructor (name, no) {this.name = name;if (no) this.no = no;}introduce () {return `์๋ ํ์ธ์, ${this.no}ํธ ${this.name}์ ์ ๋๋ค!`;}order (name) {return `${this.menu[name]}์์ ๋๋ค.`}}class ConceptYalcoChicken extends YalcoChicken {#word = '';constructor (name, no, word) {super(name, no);this.#word = word;}introWithConcept () {return super.introduce() + ' ' + this.#word;}order (name) {return super.order(name) + ' ' + this.#word;}}โconst pikaChain = new ConceptYalcoChicken('๋๋ด', 50, 'ํผ์นดํผ์นด~');console.log(pikaChain);console.log(pikaChain.introWithConcept());console.log(pikaChain.order('ํ๋ผ์ด๋'));- super๋ ๋ค๋ฅธ ํด๋์ค์์ ์์๋ฐ์ ํด๋์ค์์๋ง ์ฌ์ฉ ๊ฐ๋ฅ
- ์์ ํด๋์ค์ constructor ๋ด์์๋ ๋ถ๋ชจ ํด๋์ค์ constructor๋ฅผ ๊ฐ๋ฆฌํด
- ์์ ํด๋์ค์ ๋ฉ์๋ ๋ด์์๋ ๋ถ๋ชจ ํด๋์ค๋ฅผ ๊ฐ๋ฆฌํด
- ๋ถ๋ชจ ํด๋์ค์ constructor๋ ๋ฉ์๋์ ์ถ๊ฐ์ ์ธ ๋์์ ๋ฃ๊ธฐ ์ํด ์ฌ์ฉ
๐ก ์ ์ ๋ฉ์๋์์
class YalcoChicken {static brand = '์์ฝ์นํจ';static contact () {console.log(`${this.brand}์ ๋๋ค. ๋ฌด์์ ๋์๋๋ฆด๊น์?`);}}โclass ConceptYalcoChicken extends YalcoChicken {static contact () {super.contact();console.log('์ปจ์ ๊ฐ๋งน๋ฌธ์๋ ํํ์ด์ง๋ฅผ ์ฐธ์กฐํ์ธ์.');}}โConceptYalcoChicken.contact();'์น > JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ