๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“Œ Language/Typescript

[TS] Extends์™€ Implements ์ฐจ์ด (Difference between extends and implements)

by Fomagran ๐Ÿ’ป 2022. 4. 2.
728x90
๋ฐ˜์‘ํ˜•

 

์•ˆ๋…•ํ•˜์„ธ์š” Foma ๐Ÿ’ป ์ž…๋‹ˆ๋‹ค.

 

์˜ค๋Š˜์€ TS์—์„œ ํด๋ž˜์Šค ์˜†์— extends์™€ implements๊ฐ€ ์ˆœ๊ฐ„ ํ—ท๊ฐˆ๋ ค์„œ ์ •ํ™•ํžˆ ๊ธฐ์–ตํ•˜๊ธฐ ์œ„ํ•ด ์ •๋ฆฌํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

๋ฐ”๋กœ ์‹œ์ž‘ํ• ๊ฒŒ์š”~


Extends

 

์ƒ์†๋ฐ›๊ณ ์ž ํ•˜๋Š” ๋ถ€๋ชจ ํด๋ž˜์Šค๋ฅผ ๋ช…์‹œํ•˜๋Š” ๊ฒƒ.

 

์ฆ‰, extends์— ์›ํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ๋ช…์‹œํ•˜๋ฉด ํ•ด๋‹น ํด๋ž˜์Šค์˜ ํ”„๋กœํผํ‹ฐ์™€ ๋ฉ”์„œ๋“œ๋ฅผ ๋”ฐ๋กœ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์•„๋„ ์ธ์Šคํ„ด์Šค์—์„œ ์ž์œ ๋กญ๊ฒŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

 

์‰ฝ๊ฒŒ ๋งํ•ด  "๋ถ€๋ชจ๊ฐ€ ๊ฐ€์ง„ ๊ฑฐ ๋„ˆ ๋ง˜๋Œ€๋กœ ์จ๋„ ๋ผ~" ๋ผ๋Š” ๊ฒƒ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

์•„๋ž˜์™€ ๊ฐ™์ด ๋ถ€๋ชจ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด ์ค๋‹ˆ๋‹ค.

 

class Parent {
  public lastName: string = "An";

  public speakKorean() {
    console.log("์•ˆ๋…•ํ•˜์„ธ์š”");
  }

  public eatWithChopsticks() {
    console.log("์ “๊ฐ€๋ฝ์œผ๋กœ ๋จน๊ธฐ");
  }
}

 

์ž์‹ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด ์•„๋ž˜์™€ ๊ฐ™์ด extends๋ฅผ ํ†ตํ•ด Parent๋ฅผ ์ƒ์† ๋ฐ›์•„์ค๋‹ˆ๋‹ค.

 

class Child extends Parent {}

 

child ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•ด์ฃผ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๋ถ€๋ชจ ํ”„๋กœํผํ‹ฐ์™€ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

let child = new Child();
console.log(child.lastName); //An
child.speakKorean(); //์•ˆ๋…•ํ•˜์„ธ์š”
child.eatWithChopsticks(); //์ “๊ฐ€๋ฝ์œผ๋กœ ๋จน๊ธฐ

Implements

 

๋ฏธ๋ฆฌ ์ถ”์ƒํ™” ๋œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ฑ„ํƒํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ

 

์ƒ์†(extends)์™€ ๋‹ค๋ฅด๊ฒŒ implements๋กœ ์–ด๋–ค ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ฑ„ํƒํ•˜๋ฉด ์ถ”์ƒํ™” ๋œ ๋ฉ”์„œ๋“œ๋‚˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ๋ฐ˜๋“œ์‹œ ๊ตฌํ˜„ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

์‰ฝ๊ฒŒ ๋งํ•˜๋ฉด "ํ•ด๋‹น ํด๋ž˜์Šค๋Š” ์ด ์ธํ„ฐํŽ˜์ด์Šค ๋ชจ์–‘์ด๋‹ˆ๊น ๋ฐ˜๋“œ์‹œ ์ด ์กฐ๊ฑด์„ ๊ฐ€์ ธ์•ผ ํ•ด!" ๋ผ๋Š” ๊ฒƒ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ๋žŒ์ด๋ผ๋ฉด ๋ฐ˜๋“œ์‹œ ๊ฐ€์ ธ์•ผ ํ•  ๋ฉ”์„œ๋“œ์™€ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ •์˜ํ•ด ๋†“์Šต๋‹ˆ๋‹ค.

 

interface Person {
  name: string;
  think(): void;
  walk(): void;
  eat(): void;
}

 

Person์„ ์œ„ Child ํด๋ž˜์Šค์— ์ฑ„ํƒํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด Child ์•„๋ž˜์— Person ํ˜•ํƒœ์˜ ํ”„๋กœํผํ‹ฐ์™€ ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๋ผ๊ณ  ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

 

 

Person์— ์ •์˜๋œ ๋ฉ”์„œ๋“œ์™€ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ง์ ‘ ๊ตฌํ˜„ํ•ด ์ค๋‹ˆ๋‹ค.

 

class Child implements Person {
  name: string = "Fomagran";
  
  think(): void {
    console.log("์ƒ๊ฐํ•˜๊ธฐ");
  }
  walk(): void {
    console.log("๊ฑท๊ธฐ");
  }
  eat(): void {
    console.log("๋จน๊ธฐ");
  }
}

 

์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌํ˜„๋œ ๋ฉ”์„œ๋“œ์™€ ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

const child = new Child()
console.log(child.name); // Fomagran
child.think() //์ƒ๊ฐํ•˜๊ธฐ
child.walk() //๊ฑท๊ธฐ
child.eat() //๋จน๊ธฐ

์˜ค๋Š˜์€ ์ด๋ ‡๊ฒŒ extends์™€ implements์˜ ์ฐจ์ด์ ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์•˜๋Š”๋ฐ์š”.

 

extends๋Š” ์ด๋ฏธ ๊ตฌํ˜„๋œ ๋ฉ”์„œ๋“œ์™€ ํ”„๋กœํผํ‹ฐ๋ฅผ ํŽธํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ๋•Œ ์“ฐ๋ฉด ์œ ์šฉํ•  ๊ฑฐ ๊ฐ™๊ณ , implements๋Š” ์–ด๋– ํ•œ ์กฐ๊ฑด์„ ๊ฐ•์ œํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ์œ ์šฉํ•  ๊ฒƒ ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.

 

ํ˜น์‹œ๋ผ๋„ ํ‹€๋ฆฐ ์ ์ด ์žˆ๊ฑฐ๋‚˜ ๊ถ๊ธˆํ•˜์‹  ์ ์ด ์žˆ๋‹ค๋ฉด ์•„๋ž˜ ๋Œ“๊ธ€๋กœ ์•Œ๋ ค์ฃผ์„ธ์š”!

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€