ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [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

    ๐Ÿ‘‰ MDN ๋ฌธ์„œ ๋ณด๊ธฐ

    ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ 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();
     
Designed by Tistory.