250x250
Rainbow๐ŸŒˆCoder
My dev Note๐Ÿ“’
Rainbow๐ŸŒˆCoder
์ „์ฒด ๋ฐฉ๋ฌธ์ž
์˜ค๋Š˜
์–ด์ œ
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (411)
    • ๊ณต์ง€์‚ฌํ•ญ (0)
    • Debugger (10)
      • Visual Studio Debugger (1)
      • Chrome DevTools (3)
      • Visual Studio Code Debugger (4)
      • eclipse (1)
      • intelliJ (1)
    • OOP (2)
      • OOP (2)
    • TypeScript (54)
      • ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ TypeScript (54)
    • Javascript (87)
      • Javascript (45)
      • Node.js (19)
      • React (5)
      • FE ๊ฐœ๋ฐœํ™˜๊ฒฝ์„ค์ • (3)
      • React์™€ Node ๊ฐ™์ด ๋•Œ๋ ค์žก๊ธฐ (6)
      • next.js (2)
      • pixi.js (7)
    • ๋งˆํฌ์—… (23)
      • Html & Css (23)
    • C# (80)
      • C# (12)
      • ์ด๊ฒƒ์ด C#์ด๋‹ค (68)
    • C++ (30)
      • c++ (27)
      • win api (3)
    • Unity (18)
      • Unity(๊ธฐ์ดˆ) (8)
      • Unity(C#์ค‘๊ธ‰) (5)
      • ์œ ๋‹ˆํ‹ฐ ํฌํ†ค(๋„คํŠธ์›Œํฌ) (4)
      • unity c# MyCode (1)
    • Java & Spring (29)
      • Java (11)
      • ์Šคํ”„๋ง (8)
      • Java Algorithm (9)
      • Javs Data Structures (1)
    • ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ (15)
      • ์ž๋ฃŒ๊ตฌ์กฐ (5)
      • ์•Œ๊ณ ๋ฆฌ์ฆ˜ (10)
    • ํ˜•์ƒ๊ด€๋ฆฌ (15)
      • Git (11)
      • ์†Œ์ŠคํŠธ๋ฆฌ (3)
    • ๊ทธ๋ž˜ํ”ฝ์Šค (7)
      • WebGl (7)
    • AWS (3)
      • aws (3)
    • ๋ฆฌ๋ˆ…์Šค (5)
      • ๋ฆฌ๋ˆ…์Šค (5)
    • ์ฑ… ๋ฆฌ๋ทฐ (13)
      • ํด๋ฆฐ์ฝ”๋“œ(์ฑ…๋ฆฌ๋ทฐ) (3)
      • ์œ ์ง€๋ณด์ˆ˜๊ฐ€๋Šฅํ•œ์ฝ”๋”ฉ์˜๊ธฐ์ˆ C#ํŽธ(์ฑ…๋ฆฌ๋ทฐ) (1)
      • ๋ฆฌํŒฉํ† ๋ง(์ž๋ฐ”์Šคํฌ๋ฆฝํŠธํŒ) (9)
    • Server (2)
      • ๊ฒŒ์ž„ ์„œ๋ฒ„(๋„คํŠธ์›Œํฌ, ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ,OS) (2)
    • ์„ค๊ณ„, ์•„ํ‚คํ…์ณ (4)
    • ํŒŒ์ด์ฌ (5)
    • ๋””์ž์ธํŒจํ„ด (2)
    • mocha (2)
    • Jest (1)
    • Spine (1)
    • ์ธ๊ณต์ง€๋Šฅ (1)
      • ํ˜ผ์ž๊ณต๋ถ€ํ•˜๋Š”๋จธ์‹ ๋Ÿฌ๋‹+๋”ฅ๋Ÿฌ๋‹ (1)

๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

  • ํ™ˆ
  • ํƒœ๊ทธ
  • ๋ฐฉ๋ช…๋ก

๊ณต์ง€์‚ฌํ•ญ

์ธ๊ธฐ ๊ธ€

ํƒœ๊ทธ

  • MySQL
  • ใ…ฃใ„ท
  • ์œ„์ž„
  • ์ปดํฌ์ง€์…˜

์ตœ๊ทผ ๋Œ“๊ธ€

์ตœ๊ทผ ๊ธ€

ํ‹ฐ์Šคํ† ๋ฆฌ

hELLO ยท Designed By ์ •์ƒ์šฐ.
Rainbow๐ŸŒˆCoder

My dev Note๐Ÿ“’

[ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ์ปคํ”ผ๋จธ์‹ ํด๋ž˜์Šค <-์ƒ์†- ์นดํŽ˜๋ผ๋–ผ๋จธ์‹ ํด๋ž˜์Šค
TypeScript/ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ TypeScript

[ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ์ปคํ”ผ๋จธ์‹ ํด๋ž˜์Šค <-์ƒ์†- ์นดํŽ˜๋ผ๋–ผ๋จธ์‹ ํด๋ž˜์Šค

2022. 4. 21. 19:00
728x90

์ปคํ”ผ๋จธ์‹  ํด๋ž˜์Šค๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์žˆ๋‹ค.

type CoffeeCup = {
  orderShot: number;
  hasMilk: boolean;
};

interface CoffeeMaker {
  makeCoffee(shots: number): CoffeeCup;
}


//์œ„ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ํด๋ž˜์Šค์ด๋‹ค.
class CoffeMachine implements CoffeeMaker {
  private static ONE_SHOT_CAPSULE: number = 3;
  private static ONE_CUP_WATER: number = 10;

  static makeMachine(beans: number, water: number): CoffeMachine {
    return new CoffeMachine(beans, water);
  }

  constructor(private capsule: number = 3, private water: number = 10) {}

  makeCoffee(orderShot: number): CoffeeCup {
    this.checkMaterials(orderShot);
    console.log("์บก์А์“ฐ๊ธฐ ์ „ " + this.capsule);
    this.useCapsule(orderShot);
    console.log("๋ฌผ์“ฐ๊ธฐ ์ „ " + this.water);
    this.useWater();
    this.heatWater();
    return this.extract(orderShot);
  }

  private checkMaterials(orderShot: number) {
    if (this.capsule < orderShot * CoffeMachine.ONE_SHOT_CAPSULE) {
      throw new Error(`์ปคํ”ผ์บก์А ๋ถ€์กฑ!!! ํ˜„์žฌ ๋ณด์œ  ์บก์А : ${this.capsule}`);
    }
    if (this.water < CoffeMachine.ONE_CUP_WATER) {
      throw new Error(`๋ฌผ ๋ถ€์กฑ!!! ๋‚จ์€ ๋ฌผ ${this.water}`);
    }
  }
  clean(): void {
    console.log("์ปคํ”ผ๋จธ์‹  ํ–‰๊ตฌ๋Š” ์ค‘!");
  }

  private extract(shots: number): CoffeeCup {
    return {
      orderShot: shots,
      hasMilk: false,
    };
  }
  private heatWater() {
    console.log("๋ฌผ ๋“์ด๋Š” ์ค‘...");
  }

  private useCapsule(shot: number) {
    console.log("์บก์А์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.");
    this.capsule -= shot * CoffeMachine.ONE_SHOT_CAPSULE;
    console.log("์บก์А ์“ฐ๊ณ  ๋‚œ ํ›„" + this.capsule);
  }

  private useWater() {
    console.log("๋ฌผ์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.");
    this.water -= CoffeMachine.ONE_CUP_WATER;
    console.log("๋ฌผ์“ฐ๊ณ  ๋‚œ ํ›„ " + this.water);
  }

  addCapsule(capsule: number) {
    this.capsule += capsule;
  }

  fulfillWater(water: number) {
    this.water += water;
  }

  get Water(): number {
    return this.water;
  }

  get Capsule(): number {
    return this.capsule;
  }
}

 

์นดํŽ˜๋ผํ…Œ๋จธ์‹  ํด๋ž˜์Šค๋„ ๋งŒ๋“ค์–ด์ค€๋‹ค.

 

์ปคํ”ผ๋จธ์‹  ํด๋ž˜์Šค ๋‚ด์šฉ ์ญˆ์šฑ ๋ณต๋ถ™ํ•ด์ค€ ๋‹ค์Œ์— 

//return this.extract(orderShot); ์š” ์ฝ”๋“œ ์ฃผ์„์ฒ˜๋ฆฌ ํ•ด์ฃผ๊ณ 

    return { ...coffee, hasMilk: true }; ์š” ์ฝ”๋“œ๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ๊ณ  

 

์Šคํƒœํ‹ฑ ๋ณ€์ˆ˜ ์•ž์— ์‹๋ณ„์ž๋ฅผ ์นดํŽ˜๋ผ๋–ผ๋จธ์‹ ์œผ๋กœ ์‹น ๋‹ค ๋ฐ”๊ฟ”์ฃผ๋ฉด 

 

ex)

CoffeMachine.ONE_SHOT_CAPSULE ->

CafeLatteMachine.ONE_SHOT_CAPSULE

class CafeLatteMachine {
  private static ONE_SHOT_CAPSULE: number = 3;
  private static ONE_CUP_WATER: number = 10;

  static makeMachine(beans: number, water: number): CoffeMachine {
    return new CoffeMachine(beans, water);
  }

  constructor(private capsule: number = 3, private water: number = 10) {}

  makeCoffee(orderShot: number): CoffeeCup {
    this.checkMaterials(orderShot);
    console.log("์บก์А์“ฐ๊ธฐ ์ „ " + this.capsule);
    this.useCapsule(orderShot);
    console.log("๋ฌผ์“ฐ๊ธฐ ์ „ " + this.water);
    this.useWater();
    this.heatWater();
    let coffee = this.extract(orderShot);
    return { ...coffee, hasMilk: true };
    //return this.extract(orderShot);
  }

  private checkMaterials(orderShot: number) {
    if (this.capsule < orderShot * CafeLatteMachine.ONE_SHOT_CAPSULE) {
      throw new Error(`์ปคํ”ผ์บก์А ๋ถ€์กฑ!!! ํ˜„์žฌ ๋ณด์œ  ์บก์А : ${this.capsule}`);
    }
    if (this.water < CafeLatteMachine.ONE_CUP_WATER) {
      throw new Error(`๋ฌผ ๋ถ€์กฑ!!! ๋‚จ์€ ๋ฌผ ${this.water}`);
    }
  }
  clean(): void {
    console.log("์ปคํ”ผ๋จธ์‹  ํ–‰๊ตฌ๋Š” ์ค‘!");
  }

  private extract(shots: number): CoffeeCup {
    return {
      orderShot: shots,
      hasMilk: false,
    };
  }
  private heatWater() {
    console.log("๋ฌผ ๋“์ด๋Š” ์ค‘...");
  }

  private useCapsule(shot: number) {
    console.log("์บก์А์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.");
    this.capsule -= shot * CafeLatteMachine.ONE_SHOT_CAPSULE;
    console.log("์บก์А ์“ฐ๊ณ  ๋‚œ ํ›„" + this.capsule);
  }

  private useWater() {
    console.log("๋ฌผ์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.");
    this.water -= CafeLatteMachine.ONE_CUP_WATER;
    console.log("๋ฌผ์“ฐ๊ณ  ๋‚œ ํ›„ " + this.water);
  }

  addCapsule(capsule: number) {
    this.capsule += capsule;
  }

  fulfillWater(water: number) {
    this.water += water;
  }

  get Water(): number {
    return this.water;
  }

  get Capsule(): number {
    return this.capsule;
  }
}

 

...

์ฝ”๋“œ๋ฅผ ๊ฐ™์ด ๋ณด๋ฉด ์—„์ฒญ ๊ธธ๊ณ , ์ฝ”๋“œ ์ค‘๋ณต๋„ ์‹ฌํ•˜๋‹ค(๋”๋Ÿฝ๋‹ค!)

//   //์ปคํ”ผ๋จธ์‹  ๋งŒ๋“ค๊ธฐ
type CoffeeCup = {
  orderShot: number;
  hasMilk: boolean;
};

interface CoffeeMaker {
  makeCoffee(shots: number): CoffeeCup;
}

//์œ„ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ํด๋ž˜์Šค์ด๋‹ค.
class CoffeMachine implements CoffeeMaker {
  private static ONE_SHOT_CAPSULE: number = 3;
  private static ONE_CUP_WATER: number = 10;

  static makeMachine(beans: number, water: number): CoffeMachine {
    return new CoffeMachine(beans, water);
  }

  constructor(private capsule: number = 3, private water: number = 10) {}

  makeCoffee(orderShot: number): CoffeeCup {
    this.checkMaterials(orderShot);
    console.log("์บก์А์“ฐ๊ธฐ ์ „ " + this.capsule);
    this.useCapsule(orderShot);
    console.log("๋ฌผ์“ฐ๊ธฐ ์ „ " + this.water);
    this.useWater();
    this.heatWater();
    return this.extract(orderShot);
  }

  private checkMaterials(orderShot: number) {
    if (this.capsule < orderShot * CoffeMachine.ONE_SHOT_CAPSULE) {
      throw new Error(`์ปคํ”ผ์บก์А ๋ถ€์กฑ!!! ํ˜„์žฌ ๋ณด์œ  ์บก์А : ${this.capsule}`);
    }
    if (this.water < CoffeMachine.ONE_CUP_WATER) {
      throw new Error(`๋ฌผ ๋ถ€์กฑ!!! ๋‚จ์€ ๋ฌผ ${this.water}`);
    }
  }
  clean(): void {
    console.log("์ปคํ”ผ๋จธ์‹  ํ–‰๊ตฌ๋Š” ์ค‘!");
  }

  private extract(shots: number): CoffeeCup {
    return {
      orderShot: shots,
      hasMilk: false,
    };
  }
  private heatWater() {
    console.log("๋ฌผ ๋“์ด๋Š” ์ค‘...");
  }

  private useCapsule(shot: number) {
    console.log("์บก์А์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.");
    this.capsule -= shot * CoffeMachine.ONE_SHOT_CAPSULE;
    console.log("์บก์А ์“ฐ๊ณ  ๋‚œ ํ›„" + this.capsule);
  }

  private useWater() {
    console.log("๋ฌผ์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.");
    this.water -= CoffeMachine.ONE_CUP_WATER;
    console.log("๋ฌผ์“ฐ๊ณ  ๋‚œ ํ›„ " + this.water);
  }

  addCapsule(capsule: number) {
    this.capsule += capsule;
  }

  fulfillWater(water: number) {
    this.water += water;
  }

  get Water(): number {
    return this.water;
  }

  get Capsule(): number {
    return this.capsule;
  }
}

const maker: CoffeMachine = CoffeMachine.makeMachine(32, 100);

//์•„๋งˆ์ถ”์–ด์™€ ํ”„๋กœ๋Š” ๋™์ผํ•œ ์ปคํ”ผ๋จธ์‹ ์„ ๋ฐ›์•˜์Œ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ ,
//amateur๋Š” ํด๋ž˜์ŠคAmateurUser๊ฐ€ ์ธ์ž๋ฅผ ๋ฐ›์„ ๋•Œ CoffeeMaker ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฐ›์•„์˜ค๊ณ ,
//pro๋Š” ํด๋ž˜์ŠคProBarista๊ฐ€ ์ธ์ž๋ฅผ ๋ฐ›์„ ๋•Œ CommercialCoffeeMaker ์•ˆํ† ํŒจ์•„์ˆ˜๋ฃฐ ๊ธฐ์ค€์œผ๋กœ ๋ฐ›์•„์˜ค๊ธฐ ๋•Œ๋ฌธ์—,
//๊ฐ๊ฐ ์ธํ„ฐํŽ˜์ด์Šค์— ์ œ์•ฝ๋œ ํ•ญ๋ชฉ๋“ค๋งŒ ์“ธ ์ˆ˜ ์žˆ๊ฒŒ๋œ๋‹ค.(์ˆœ์‹๊ฐ„์— ๊ฐ€์šฉ ๋ฒ”์œ„๊ฐ€ ๋‹ฌ๋ผ์ง!!!)

console.log("/* ์ฒซ๋ฒˆ์งธ ์ฃผ๋ฌธ */");
let normalMachine = new CoffeMachine(10, 10);
console.log(normalMachine.Water); //10
console.log(normalMachine.Capsule); //10
let order1 = normalMachine.makeCoffee(2);
console.log(order1); //{ orderShot: 2 }
console.log("๋ฌผ ์ƒํƒœ" + normalMachine.Water); //0
console.log("์บก์А ์ƒํƒœ" + normalMachine.Capsule); //4

console.log("/* ๋‘๋ฒˆ์งธ ์ฃผ๋ฌธ */");
normalMachine.fulfillWater(100);
let order2 = normalMachine.makeCoffee(1);
console.log(order2); //{ orderShot: 1 }
console.log("๋ฌผ ์ƒํƒœ" + normalMachine.Water); //90
console.log("์บก์А ์ƒํƒœ" + normalMachine.Capsule); //1

console.log("/* ์„ธ๋ฒˆ์งธ ์ฃผ๋ฌธ */");
let order3 = normalMachine.makeCoffee(3);
console.log(order3); //Error: ์ปคํ”ผ์บก์А ๋ถ€์กฑ!!! ํ˜„์žฌ ๋ณด์œ  ์บก์А : 1

class CafeLatteMachine {
  private static ONE_SHOT_CAPSULE: number = 3;
  private static ONE_CUP_WATER: number = 10;

  static makeMachine(beans: number, water: number): CoffeMachine {
    return new CoffeMachine(beans, water);
  }

  constructor(private capsule: number = 3, private water: number = 10) {}

  makeCoffee(orderShot: number): CoffeeCup {
    this.checkMaterials(orderShot);
    console.log("์บก์А์“ฐ๊ธฐ ์ „ " + this.capsule);
    this.useCapsule(orderShot);
    console.log("๋ฌผ์“ฐ๊ธฐ ์ „ " + this.water);
    this.useWater();
    this.heatWater();
    let coffee = this.extract(orderShot);
    return { ...coffee, hasMilk: true };
    //return this.extract(orderShot);
  }

  private checkMaterials(orderShot: number) {
    if (this.capsule < orderShot * CafeLatteMachine.ONE_SHOT_CAPSULE) {
      throw new Error(`์ปคํ”ผ์บก์А ๋ถ€์กฑ!!! ํ˜„์žฌ ๋ณด์œ  ์บก์А : ${this.capsule}`);
    }
    if (this.water < CafeLatteMachine.ONE_CUP_WATER) {
      throw new Error(`๋ฌผ ๋ถ€์กฑ!!! ๋‚จ์€ ๋ฌผ ${this.water}`);
    }
  }
  clean(): void {
    console.log("์ปคํ”ผ๋จธ์‹  ํ–‰๊ตฌ๋Š” ์ค‘!");
  }

  private extract(shots: number): CoffeeCup {
    return {
      orderShot: shots,
      hasMilk: false,
    };
  }
  private heatWater() {
    console.log("๋ฌผ ๋“์ด๋Š” ์ค‘...");
  }

  private useCapsule(shot: number) {
    console.log("์บก์А์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.");
    this.capsule -= shot * CafeLatteMachine.ONE_SHOT_CAPSULE;
    console.log("์บก์А ์“ฐ๊ณ  ๋‚œ ํ›„" + this.capsule);
  }

  private useWater() {
    console.log("๋ฌผ์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.");
    this.water -= CafeLatteMachine.ONE_CUP_WATER;
    console.log("๋ฌผ์“ฐ๊ณ  ๋‚œ ํ›„ " + this.water);
  }

  addCapsule(capsule: number) {
    this.capsule += capsule;
  }

  fulfillWater(water: number) {
    this.water += water;
  }

  get Water(): number {
    return this.water;
  }

  get Capsule(): number {
    return this.capsule;
  }
}

 ๋ฌธ์ œ์ƒํ™ฉ์„ ๊ณ ์น˜๊ธฐ ์œ„ํ•œ ์ด์ œ๋ถ€ํ„ฐ '์ƒ์†'์„ ๋ณธ๊ฒฉ์ ์œผ๋กœ ๋„์ž…ํ•ด๋ณธ๋‹ค.

 

์ƒ์†์„ ํ†ตํ•ด ์ฝ”๋“œ์˜ ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’์—ฌ๋ณด์ž

ํด๋ž˜์Šค ์ƒ์†์€ extends ํ‚ค์›Œ๋“œ,

์ธํ„ฐํŽ˜์ด์Šค ์ƒ์†์€ implements ํ‚ค์›Œ๋“œ๋ฅผ ์จ์•ผํ•œ๋‹ค. 

class CafeLatteMachine extends CoffeMachine {}

1. ์นดํŽ˜๋ผ๋–ผ๋จธ์‹ ์€ ์ปคํ”ผ๋จธ์‹ ์„ ์ƒ์†ํ•œ๋‹ค.

2. ์ปคํ”ผ๋จธ์‹  ์ƒ์„ฑ์ž ์•ž์— public ํ˜น์€ protected ํ‚ค์›Œ๋“œ ๊ธฐ์žฌ

 

<์ผ๋‹จ๋ฝ๋œ ์ฝ”๋“œ>

//   //์ปคํ”ผ๋จธ์‹  ๋งŒ๋“ค๊ธฐ
type CoffeeCup = {
  orderShot: number;
  hasMilk: boolean;
};

interface CoffeeMaker {
  makeCoffee(shots: number): CoffeeCup;
}

//์œ„ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ํด๋ž˜์Šค์ด๋‹ค.
class CoffeMachine implements CoffeeMaker {
  private static ONE_SHOT_CAPSULE: number = 3;
  private static ONE_CUP_WATER: number = 10;

  static makeMachine(beans: number, water: number): CoffeMachine {
    return new CoffeMachine(beans, water);
  }

  public constructor(private capsule: number = 3, private water: number = 10) {}

  makeCoffee(orderShot: number): CoffeeCup {
    this.checkMaterials(orderShot);
    console.log("์บก์А์“ฐ๊ธฐ ์ „ " + this.capsule);
    this.useCapsule(orderShot);
    console.log("๋ฌผ์“ฐ๊ธฐ ์ „ " + this.water);
    this.useWater();
    this.heatWater();
    return this.extract(orderShot);
  }

  private checkMaterials(orderShot: number) {
    if (this.capsule < orderShot * CoffeMachine.ONE_SHOT_CAPSULE) {
      throw new Error(`์ปคํ”ผ์บก์А ๋ถ€์กฑ!!! ํ˜„์žฌ ๋ณด์œ  ์บก์А : ${this.capsule}`);
    }
    if (this.water < CoffeMachine.ONE_CUP_WATER) {
      throw new Error(`๋ฌผ ๋ถ€์กฑ!!! ๋‚จ์€ ๋ฌผ ${this.water}`);
    }
  }
  clean(): void {
    console.log("์ปคํ”ผ๋จธ์‹  ํ–‰๊ตฌ๋Š” ์ค‘!");
  }

  private extract(shots: number): CoffeeCup {
    return {
      orderShot: shots,
      hasMilk: false,
    };
  }
  private heatWater() {
    console.log("๋ฌผ ๋“์ด๋Š” ์ค‘...");
  }

  private useCapsule(shot: number) {
    console.log("์บก์А์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.");
    this.capsule -= shot * CoffeMachine.ONE_SHOT_CAPSULE;
    console.log("์บก์А ์“ฐ๊ณ  ๋‚œ ํ›„" + this.capsule);
  }

  private useWater() {
    console.log("๋ฌผ์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.");
    this.water -= CoffeMachine.ONE_CUP_WATER;
    console.log("๋ฌผ์“ฐ๊ณ  ๋‚œ ํ›„ " + this.water);
  }

  addCapsule(capsule: number) {
    this.capsule += capsule;
  }

  fulfillWater(water: number) {
    this.water += water;
  }

  get Water(): number {
    return this.water;
  }

  get Capsule(): number {
    return this.capsule;
  }
}

class CafeLatteMachine extends CoffeMachine {}

const machine = new CoffeMachine(100, 100);
const latteMachine = new CafeLatteMachine(100, 100);

ํ˜„์žฌ ์ƒํƒœ. API๋“ค ์ž˜ ๋ฌผ๋ ค๋ฐ›์€ ๋ชจ์Šต!

const machine = new CoffeMachine(100, 100);
const latteMachine = new CafeLatteMachine(100, 100);
const latte = latteMachine.makeCoffee(10);
console.log(latte);

<์ถœ๋ ฅ๊ฒฐ๊ณผ> //์ฐธ๊ณ ๋กœ ๋ฌผ์€ ์ƒท์— ์ƒ๊ด€์—†์ด ํ•œ์ž”์ด๋ฉด ๋ฌด์กฐ๊ฑด -10(๋‚ด๊ฐ€ ๋งŒ๋“ค์—ˆ๋Š”๋ฐ ์ž ์‹œ ํ—ท๊ฐˆ...)

์บก์А์“ฐ๊ธฐ ์ „ 100
์บก์А์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
์บก์А ์“ฐ๊ณ  ๋‚œ ํ›„70
๋ฌผ์“ฐ๊ธฐ ์ „ 100
๋ฌผ์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
๋ฌผ์“ฐ๊ณ  ๋‚œ ํ›„ 90
๋ฌผ ๋“์ด๋Š” ์ค‘...
{ orderShot: 10, hasMilk: false } // ๋‹น์—ฐํžˆ ์•„์ง ๋ฐ€ํฌ๋Š” ๋“ค์–ด๊ฐ€์ง€ ์•Š์€ ๊ทธ๋ƒฅ ์ปคํ”ผ๊ฐ€ ๋‚˜์™”๋‹ค~ ์ด์ œ ์ด๊ฑธ ๊ณ ์ณ์ฃผ์ž

 

์ž์‹ํด๋ž˜์Šค์—์„œ ๋ถ€๋ชจํด๋ž˜์Šค์— ์žˆ๋Š” ํ•จ์ˆ˜๋ฅผ ๋ฎ์–ด์”Œ์šธ ์ˆ˜๊ฐ€ ์žˆ๋‹ค. ์ด๊ฒƒ์ด ๋ฐ”๋กœ ์˜ค๋ฒ„๋ผ์ด๋”ฉ(์˜ค๋ฒ„๋ผ์ดํŒ…)์ด๋‹ค.

 

class CafeLatteMachine extends CoffeMachine {
  makeCoffee(orderShot: number): CoffeeCup {
    this.checkMaterials(orderShot);
    console.log("์บก์А์“ฐ๊ธฐ ์ „ " + this.capsule);
    this.useCapsule(orderShot);
    console.log("๋ฌผ์“ฐ๊ธฐ ์ „ " + this.water);
    this.useWater();
    this.heatWater();
    let latte = this.extract(orderShot);
    return { ...latte, hasMilk: true };
  }
}

์ผ๋‹จ์€ ์œ„์™€ ๊ฐ™์ด ๋ณ€๊ฒฝํ•˜๋ฉด ๋ญ”๊ฐ€ ์ด์ƒํ•˜์ง€ ์•Š์€๊ฐ€?

๋˜ ์ฝ”๋“œ์ค‘๋ณต์ด๋‹ค!

์ด๋Ÿด ๋•Œ, super๋ผ๋Š” ๊ฒƒ์„ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค.

๋ถ€๋ชจ์—์„œ ๋งŒ๋“ค์–ด์ง„ ์ปคํ”ผ์˜ ๋ฆฌํ„ด๊ฐ’์ธ ๊ฐ์ฒด์˜ hasMilk ํ”„๋กœํผํ‹ฐ boolean๊ฐ’๋งŒ ๋ฐ”๊ฟ”์ฃผ๊ธฐ ์œ„ํ•˜์—ฌ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ณ€๊ฒฝํ•ด์„œ ์ž‘์„ฑํ•ด์ค€๋‹ค.

class CafeLatteMachine extends CoffeMachine {
  makeCoffee(orderShot: number): CoffeeCup {
    const coffee = super.makeCoffee(orderShot); //๊ธฐ๋ณธ์ ์ธ ์บก์А ์†Œ๋น„, ๋ฌผ ์†Œ๋น„, ๋ฌผ ๊ฐ€์—ด ๋‹ค ๋๋‚˜๊ณ  ์™„์„ฑ๋œ ์ปคํ”ผ๊ฐ€ ๋ฐ˜ํ™˜
    //๋ถ€๋ชจ์—์„œ ๋ฐ›์•„์˜จ ์ปคํ”ผ ์™„์„ฑํ’ˆ์— ์šฐ์œ ๋งŒ ๋ถ€์–ด์ฃผ์ž!!!
    return { ...coffee, hasMilk: true };
  }
}

const machine = new CoffeMachine(100, 100);
const latteMachine = new CafeLatteMachine(100, 100);
const latte = latteMachine.makeCoffee(10);
console.log(latte);

<์ถœ๋ ฅ๊ฒฐ๊ณผ> ๊ณ„ํš๋Œ€๋กœ... ํ›Œ๋ฅญํ•œ ์ถœ๋ ฅ ๊ฒฐ๊ณผ

์บก์А์“ฐ๊ธฐ ์ „ 100
์บก์А์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
์บก์А ์“ฐ๊ณ  ๋‚œ ํ›„70
๋ฌผ์“ฐ๊ธฐ ์ „ 100
๋ฌผ์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
๋ฌผ์“ฐ๊ณ  ๋‚œ ํ›„ 90
๋ฌผ ๋“์ด๋Š” ์ค‘...
{ orderShot: 10, hasMilk: true }

 

ํ•˜์ง€๋งŒ ๋‹จ์ˆœํžˆ ํ”„๋กœํผํ‹ฐ๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” 

๊ฐ€์—ด๋œMilk๋ฅผ ๋”ฐ๋กœ ๋„ฃ์–ด์ฃผ๋Š” ํ•จ์ˆ˜๋ฅผ ์นดํŽ˜๋ผ๋–ผ ๋จธ์‹ ์ด ์ถ”๊ฐ€ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ๋” ๋ฉ‹์žˆ์ง€ ์•Š์„๊นŒ?

์ถ”๊ฐ€ํ•œ ๋’ค, ์•„๋ž˜ ์ „์ฒด ์†Œ์Šค์ฝ”๋“œ ํ•œ๋ฒˆ ๊ธฐ๋กํ•ด๋„ฃ๊ณ , ์ด์–ด์„œ ์ž‘์—…ํ•ด๋ณด๋„๋ก ํ•œ๋‹ค. 

 

<์—ฌ๊ธฐ๊นŒ์ง€ ์ง„ํ–‰๋œ ์†Œ์Šค์ฝ”๋“œ>

//   //์ปคํ”ผ๋จธ์‹  ๋งŒ๋“ค๊ธฐ
type CoffeeCup = {
  orderShot: number;
  hasMilk: boolean;
};

interface CoffeeMaker {
  makeCoffee(shots: number): CoffeeCup;
}

//์œ„ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ํด๋ž˜์Šค์ด๋‹ค.
class CoffeMachine implements CoffeeMaker {
  private static ONE_SHOT_CAPSULE: number = 3;
  private static ONE_CUP_WATER: number = 10;

  static makeMachine(beans: number, water: number): CoffeMachine {
    return new CoffeMachine(beans, water);
  }

  public constructor(private capsule: number = 3, private water: number = 10) {}

  makeCoffee(orderShot: number): CoffeeCup {
    this.checkMaterials(orderShot);
    console.log("์บก์А์“ฐ๊ธฐ ์ „ " + this.capsule);
    this.useCapsule(orderShot);
    console.log("๋ฌผ์“ฐ๊ธฐ ์ „ " + this.water);
    this.useWater();
    this.heatWater();
    return this.extract(orderShot);
  }

  private checkMaterials(orderShot: number) {
    if (this.capsule < orderShot * CoffeMachine.ONE_SHOT_CAPSULE) {
      throw new Error(`์ปคํ”ผ์บก์А ๋ถ€์กฑ!!! ํ˜„์žฌ ๋ณด์œ  ์บก์А : ${this.capsule}`);
    }
    if (this.water < CoffeMachine.ONE_CUP_WATER) {
      throw new Error(`๋ฌผ ๋ถ€์กฑ!!! ๋‚จ์€ ๋ฌผ ${this.water}`);
    }
  }
  clean(): void {
    console.log("์ปคํ”ผ๋จธ์‹  ํ–‰๊ตฌ๋Š” ์ค‘!");
  }

  private extract(shots: number): CoffeeCup {
    return {
      orderShot: shots,
      hasMilk: false,
    };
  }
  private heatWater() {
    console.log("๋ฌผ ๋“์ด๋Š” ์ค‘...");
  }

  private useCapsule(shot: number) {
    console.log("์บก์А์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.");
    this.capsule -= shot * CoffeMachine.ONE_SHOT_CAPSULE;
    console.log("์บก์А ์“ฐ๊ณ  ๋‚œ ํ›„" + this.capsule);
  }

  private useWater() {
    console.log("๋ฌผ์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.");
    this.water -= CoffeMachine.ONE_CUP_WATER;
    console.log("๋ฌผ์“ฐ๊ณ  ๋‚œ ํ›„ " + this.water);
  }

  addCapsule(capsule: number) {
    this.capsule += capsule;
  }

  fulfillWater(water: number) {
    this.water += water;
  }

  get Water(): number {
    return this.water;
  }

  get Capsule(): number {
    return this.capsule;
  }
}

// tslint:disable-next-line: max-classes-per-file
class CafeLatteMachine extends CoffeMachine {
  makeCoffee(orderShot: number): CoffeeCup {
    const coffee = super.makeCoffee(orderShot); //๊ธฐ๋ณธ์ ์ธ ์บก์А ์†Œ๋น„, ๋ฌผ ์†Œ๋น„, ๋ฌผ ๊ฐ€์—ด ๋‹ค ๋๋‚˜๊ณ  ์™„์„ฑ๋œ ์ปคํ”ผ๊ฐ€ ๋ฐ˜ํ™˜
    //๋ถ€๋ชจ์—์„œ ๋ฐ›์•„์˜จ ์ปคํ”ผ ์™„์„ฑํ’ˆ์— ์šฐ์œ ๋งŒ ๋ถ€์–ด์ฃผ์ž!!!
    this.steamMilk();
    this.pourMilk();
    return { ...coffee, hasMilk: true };
  }
  private steamMilk(): void {
    console.log("์šฐ์œ  ๋ฐ์šฐ๋Š” ์ค‘...");
  }
  private pourMilk() {
    console.log("์šฐ์œ  ๋ถ“๋Š” ์ค‘...");
  }
}

const machine = new CoffeMachine(100, 100);
const latteMachine = new CafeLatteMachine(100, 100);
const latte = latteMachine.makeCoffee(10);
console.log(latte);

<์ถœ๋ ฅ ๊ฒฐ๊ณผ> //๋ถ€๋ชจ์—์„œ ํ•œ ๋™์ž‘๋“ค์„ ์ „๋ถ€ ๊ทธ๋Œ€๋กœ ํ•œ ๋‹ค์Œ์—, ์šฐ์œ  ๋ฐ์šฐ๊ณ , ์šฐ์œ  ๋ถ€์–ด์„œ hasMilk๊ฐ€ ture์ธ ์ง„์งœ ์นดํŽ˜๋ผ๋–ผ๊ฐ€ ์™„์„ฑ๋˜์—ˆ๋‹ค!

์บก์А์“ฐ๊ธฐ ์ „ 100
์บก์А์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
์บก์А ์“ฐ๊ณ  ๋‚œ ํ›„70
๋ฌผ์“ฐ๊ธฐ ์ „ 100
๋ฌผ์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
๋ฌผ์“ฐ๊ณ  ๋‚œ ํ›„ 90
๋ฌผ ๋“์ด๋Š” ์ค‘...
์šฐ์œ  ๋ฐ์šฐ๋Š” ์ค‘...
์šฐ์œ  ๋ถ“๋Š” ์ค‘...
{ orderShot: 10, hasMilk: true }

 

์Œ... ๊ทธ๋Ÿฐ๋ฐ ์—ฌ๊ธฐ์„œ ์šฐ์œ ๋ž€ ์†์„ฑ์— ๋Œ€ํ•˜์—ฌ ์ฝ˜์†” ๋กœ๊ทธ๋งŒ ์ฐ์ง€ ๋ง๊ณ 

์‹ค์งˆ์ ์œผ๋กœ ์นดํŽ˜๋ผ๋–ผ ๋จธ์‹ ์— ์šฐ์œ ๋ฅผ ๋‹ด์„ ์ˆ˜ ์žˆ์–ด์„œ, ์šฐ์œ ๋ฅผ ์ฑ„์›Œ๋„ฃ๊ณ , ์šฐ์œ ๊ฐ€ ๋ถ€์กฑํ•˜๋ฉด ์—๋Ÿฌ๋ฅผ ๋˜์ง€๊ฒŒ๋”...

ํ•œ๋งˆ๋””๋กœ ๋”์šฑ ์‚ด์•„์žˆ๋Š” ์ž์‹ํด๋ž˜์Šค๋กœ ๋ฐ”๊ฟ€ ์ˆœ ์—†์„๊นŒ?

์‹œ๋„ํ•ด๋ณด์ž.

 

<์ตœ์ข… ์†Œ์Šค์ฝ”๋“œ> : ์ƒ์„ฑ์ž ์˜ค๋ฒ„๋ผ์ด๋”ฉ ๋ฐ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ํ•จ์ˆ˜ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ์ง„ํ–‰!

//   //์ปคํ”ผ๋จธ์‹  ๋งŒ๋“ค๊ธฐ
type CoffeeCup = {
  orderShot: number;
  hasMilk: boolean;
};

interface CoffeeMaker {
  makeCoffee(shots: number): CoffeeCup;
}

//์œ„ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ํด๋ž˜์Šค์ด๋‹ค.
class CoffeMachine implements CoffeeMaker {
  private static ONE_SHOT_CAPSULE: number = 3;
  private static ONE_CUP_WATER: number = 10;

  static makeMachine(beans: number, water: number): CoffeMachine {
    return new CoffeMachine(beans, water);
  }

  public constructor(
    protected capsule: number = 3,
    protected water: number = 10
  ) {}

  makeCoffee(orderShot: number): CoffeeCup {
    this.checkMaterials(orderShot);
    console.log("์บก์А์“ฐ๊ธฐ ์ „ " + this.capsule);
    this.useCapsule(orderShot);
    console.log("๋ฌผ์“ฐ๊ธฐ ์ „ " + this.water);
    this.useWater();
    this.heatWater();
    return this.extract(orderShot);
  }

  protected checkMaterials(orderShot: number) {
    if (this.capsule < orderShot * CoffeMachine.ONE_SHOT_CAPSULE) {
      throw new Error(`์ปคํ”ผ์บก์А ๋ถ€์กฑ!!! ํ˜„์žฌ ๋ณด์œ  ์บก์А : ${this.capsule}`);
    }
    if (this.water < CoffeMachine.ONE_CUP_WATER) {
      throw new Error(`๋ฌผ ๋ถ€์กฑ!!! ๋‚จ์€ ๋ฌผ ${this.water}`);
    }
  }
  clean(): void {
    console.log("์ปคํ”ผ๋จธ์‹  ํ–‰๊ตฌ๋Š” ์ค‘!");
  }

  private extract(shots: number): CoffeeCup {
    return {
      orderShot: shots,
      hasMilk: false,
    };
  }
  private heatWater() {
    console.log("๋ฌผ ๋“์ด๋Š” ์ค‘...");
  }

  private useCapsule(shot: number) {
    console.log("์บก์А์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.");
    this.capsule -= shot * CoffeMachine.ONE_SHOT_CAPSULE;
    console.log("์บก์А ์“ฐ๊ณ  ๋‚œ ํ›„" + this.capsule);
  }

  private useWater() {
    console.log("๋ฌผ์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.");
    this.water -= CoffeMachine.ONE_CUP_WATER;
    console.log("๋ฌผ์“ฐ๊ณ  ๋‚œ ํ›„ " + this.water);
  }

  addCapsule(capsule: number) {
    this.capsule += capsule;
  }

  fulfillWater(water: number) {
    this.water += water;
  }

  get Water(): number {
    return this.water;
  }

  get Capsule(): number {
    return this.capsule;
  }
}

// tslint:disable-next-line: max-classes-per-file
class CafeLatteMachine extends CoffeMachine {
  private static ONE_CUP_Milk: number = 2;
  public constructor(
    protected capsule: number = 3,
    protected water: number = 10,
    private milk: number
  ) {
    super(capsule, water);
  }

  makeCoffee(orderShot: number): CoffeeCup {
    const coffee = super.makeCoffee(orderShot); //๊ธฐ๋ณธ์ ์ธ ์บก์А ์†Œ๋น„, ๋ฌผ ์†Œ๋น„, ๋ฌผ ๊ฐ€์—ด ๋‹ค ๋๋‚˜๊ณ  ์™„์„ฑ๋œ ์ปคํ”ผ๊ฐ€ ๋ฐ˜ํ™˜
    //๋ถ€๋ชจ์—์„œ ๋ฐ›์•„์˜จ ์ปคํ”ผ ์™„์„ฑํ’ˆ์— ์šฐ์œ ๋งŒ ๋ถ€์–ด์ฃผ์ž!!!
    this.steamMilk();
    this.pourMilk();
    return { ...coffee, hasMilk: true };
  }

  //์˜ค๋ฒ„๋ผ์ด๋”ฉ
  checkMaterials(orderShot: number) {
    super.checkMaterials(orderShot);
    if (this.milk < CafeLatteMachine.ONE_CUP_Milk) {
      throw new Error(`์šฐ์œ  ๋ถ€์กฑ!!! ํ˜„์žฌ ๋ณด์œ  ์šฐ์œ ๋Ÿ‰ : ${this.milk}`);
    }
  }

  private steamMilk(): void {
    console.log("์šฐ์œ  ๋ฐ์šฐ๋Š” ์ค‘...");
  }

  private pourMilk() {
    console.log("์šฐ์œ  ๋ถ“๋Š” ์ค‘...");
    this.milk -= CafeLatteMachine.ONE_CUP_Milk;
    console.log("์šฐ์œ  ๋ถ“๊ณ  ๋‚œ ๋’ค " + this.milk);
  }

  public fulfillMilk(milk: number) {
    this.milk += milk;
  }

  get Milk(): number {
    return this.milk;
  }
}

const machine = new CoffeMachine(100, 100);
const latteMachine = new CafeLatteMachine(100, 100, 2);

const latte1 = latteMachine.makeCoffee(10);
console.log("์ฒซ๋ฒˆ์งธ ๋ผ๋–ผ...๋‘๊ตฌ๋‘๊ตฌ๋‘๊ตฌ...");
console.log(latte1);

console.log("์ง€๊ธˆ ์šฐ์œ  ์ƒํƒœ๋Š”? : " + latteMachine.Milk);
console.log("์šฐ์œ ๊ฐ€ ์—†์œผ๋‹ˆ๊นŒ ์ถฉ์ „ํ•ด์ฃผ์ž");
latteMachine.fulfillMilk(2);

const latte2 = latteMachine.makeCoffee(10);
console.log("๋‘๋ฒˆ์งธ ๋ผ๋–ผ~~~");
console.log(latte2);

const latte3 = latteMachine.makeCoffee(10);
console.log("์šฐ์œ ๊ฐ€ ์—†๋Š” ์ƒํƒœ์—์„œ ์„ธ๋ฒˆ์งธ ๋ผ๋–ผ๋ฅผ ๋งŒ๋“ค๋ ค๊ณ  ํ•˜๋ฉด~~?");
console.log(latte3);

<์ถœ๋ ฅ๊ฒฐ๊ณผ>

์บก์А์“ฐ๊ธฐ ์ „ 100
์บก์А์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
์บก์А ์“ฐ๊ณ  ๋‚œ ํ›„70
๋ฌผ์“ฐ๊ธฐ ์ „ 100
๋ฌผ์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
๋ฌผ์“ฐ๊ณ  ๋‚œ ํ›„ 90
๋ฌผ ๋“์ด๋Š” ์ค‘...
์šฐ์œ  ๋ฐ์šฐ๋Š” ์ค‘...
์šฐ์œ  ๋ถ“๋Š” ์ค‘...
์šฐ์œ  ๋ถ“๊ณ  ๋‚œ ๋’ค 0
์ฒซ๋ฒˆ์งธ ๋ผ๋–ผ...๋‘๊ตฌ๋‘๊ตฌ๋‘๊ตฌ...
{ orderShot: 10, hasMilk: true }
์ง€๊ธˆ ์šฐ์œ  ์ƒํƒœ๋Š”? : 0
์šฐ์œ ๊ฐ€ ์—†์œผ๋‹ˆ๊นŒ ์ถฉ์ „ํ•ด์ฃผ์ž
์บก์А์“ฐ๊ธฐ ์ „ 70
์บก์А์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
์บก์А ์“ฐ๊ณ  ๋‚œ ํ›„40
๋ฌผ์“ฐ๊ธฐ ์ „ 90
๋ฌผ์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
๋ฌผ์“ฐ๊ณ  ๋‚œ ํ›„ 80
๋ฌผ ๋“์ด๋Š” ์ค‘...
์šฐ์œ  ๋ฐ์šฐ๋Š” ์ค‘...
์šฐ์œ  ๋ถ“๋Š” ์ค‘...
์šฐ์œ  ๋ถ“๊ณ  ๋‚œ ๋’ค 0
๋‘๋ฒˆ์งธ ๋ผ๋–ผ~~~
{ orderShot: 10, hasMilk: true }
/Users/๊ฒฝ๋กœ/Desktop/๊ฒฝ๋กœ/dist/index.js
            throw new Error(`์šฐ์œ  ๋ถ€์กฑ!!! ํ˜„์žฌ ๋ณด์œ  ์šฐ์œ ๋Ÿ‰ : ${this.milk}`);
            ^

Error: ์šฐ์œ  ๋ถ€์กฑ!!! ํ˜„์žฌ ๋ณด์œ  ์šฐ์œ ๋Ÿ‰ : 0

 

 

์„ฑ๊ณต์ ์ด๋‹ค!!

 

์ด์ฒ˜๋Ÿผ ์ƒ์†์„ ์ž˜ ์ด์šฉํ•˜๋ฉด ๊ณตํ†ต์ ์ธ ๊ธฐ๋Šฅ์€ ๊ทธ๋Œ€๋กœ ์žฌ์‚ฌ์šฉํ•˜๋ฉด์„œ, 

์ž์‹ํด๋ž˜์Šค์—์„œ๋งŒ ์กฐ๊ธˆ ๋” ์ž์‹ํด๋ž˜์Šค์— ํŠนํ™”๋œ ๊ธฐ๋Šฅ๋“ค์„ ํ•  ์ˆ˜ ์žˆ๊ณ , ์ถ”๊ฐ€ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. 

๊ทธ๋ฆฌ๊ณ  super๋ผ๋Š” ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ๋ถ€๋ชจ ํด๋ž˜์Šค์— ์žˆ๋Š” ํ•จ์ˆ˜๋“ค์„ ํ˜ธ์ถœํ•˜๊ฑฐ๋‚˜ ์ ‘๊ทผํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

 

 

๋‹ค๋งŒ, ์ƒ์„ฑ์ž ์˜ค๋ฒ„๋ผ์ด๋”ฉ์€ ์ฃผ์˜ํ•  ์ ์ด ํฌ๊ฒŒ 2๊ฐœ ์žˆ๋‹ค.

๋งŒ์•ฝ ์ž์‹ ํด๋ž˜์Šค์—์„œ ๋˜ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑ์ž์—์„œ ๋ฐ›๊ณ  ์‹ถ๋‹ค๋ฉด (์ž์‹์—์„œ constructor์— ๋‹ค๋ฅธ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ๋Š” ๋“ฑ ๋‹ค๋ฅธ ์ž‘์—…์„ ์ถ”๊ฐ€ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด์„œ ์ž์‹ ํด๋ž˜์Šค ์ƒ์„ฑ์ž๋ฅผ ์Šˆํผ ํด๋ž˜์Šค ์ƒ์„ฑ์ž์™€ ๋‹ค๋ฅด๊ฒŒ ๋”ฐ๋กœ ๊ตฌํ˜„ํ•˜๊ณ ์ž ํ•  ๊ฒฝ์šฐ์—๋Š”)

์ž์‹ํด๋ž˜์Šค์—์„œ์˜ ์ƒ์„ฑ์ž(derived classes)๋Š” ๊ผญ super์„ ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค. 

 

 

 

์—๋Ÿฌ๋ฉ”์‹œ์ง€ : ํŒŒ์ƒ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๋Š” 'super' ํ˜ธ์ถœ์„ ํฌํ•จํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ts(2377)

  

์ฆ‰, ์œ„ ์บก์ฒ˜์ฒ˜๋Ÿผ ์ƒ์„ฑ์ž๋Š” super() ํ˜•์‹์œผ๋กœ ๋‹ฌ์•„์ฃผ๊ณ , ์ธ์ž ์ฑ„์›Œ์ฃผ๋Š” ์‹์œผ๋กœ ๋ถˆ๋Ÿฌ์ฃผ๋ฉด ๋œ๋‹ค.

super(capsule, water)

๋˜ ํ•˜๋‚˜ ์œ ๋…ํ•  ์ ์€ ์ž์‹ํด๋ž˜์Šค ์•ˆ์— ์“ฐ๋Š” ์ƒ์„ฑ์ž์—์„œ super()๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ ์ž์ฒด๊ฐ€ ๋ถ€๋ชจ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ฌผ๋ ค๋ฐ›์€ ์„ค๊ณ„๋„๋ผ๋Š” ๋œป์ด๋‹ค! super()๊นŒ์ง€ ์“ด ๋‹ค์Œ์— ๋งˆ์šฐ์Šค๋งŒ ์Šฌ์ฉ ์˜ฌ๋ฆฌ๋ฉด ์–ด๋–ค ์ธ์ž๋ฅผ ์ „๋‹ฌํ•ด์•ผ๋งŒ ํ•˜๋Š”์ง€ ๋‹ค ์•Œ๋ ค์ค€๋‹ค(๊ตฟ)

๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ž์‹ ํด๋ž˜์Šค ์•ˆ์—์„œ ์“ฐ๋Š” ์ƒ์„ฑ์ž ์•ˆ์— ์ตœ์†Œ super๊ฐ€ ์š”๊ตฌํ•˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์จ์ฃผ์–ด์•ผ๋งŒ ํ•œ๋‹ค.

๊ทธ๋ ‡๊ฒŒ ์ž์‹ ํด๋ž˜์Šค ์ƒ์„ฑ์ž ๋งค๊ฐœ๋ณ€์ˆ˜๋“ค ์ค‘์—์„œ ๋ถ€๋ชจ ํด๋ž˜์Šค์—์„œ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋„ ๋ฐ›์•„์ฃผ๊ณ ๋„, ์ด๋ ‡๊ฒŒ ๋ฐ›์•„์˜จ ๋ฐ์ดํ„ฐ๋ฅผ super(์—ฌ๊ธฐ์•ˆ์— ์ „๋‹ฌ์„ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.) 

 

์ฐธ๊ณ ๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ํ•œ๋ฒˆ ๋ฐ›์•„์˜จ ์ธ์ž ๊ฐ’์ด ๋ฐ”๋€” ์ผ์ด ์—†๋‹ค๋ฉด

constructor(beans: number, public readonly serialNumber : string)

readonly๋ฅผ ๋ถ™์—ฌ ์ฝ๊ธฐ ์ „์šฉ์œผ๋กœ ๋ฐ”๊พธ์–ด ์ค„ ์ˆ˜๋„ ์žˆ๋‹ค.

class CafeLatteMachine extends CoffeMachine {
  private static ONE_CUP_Milk: number = 2;

  public constructor(
    protected capsule: number = 3,
    protected water: number = 10,
    private milk: number,
    public readonly serialNumber: string
  ) {
    super(capsule, water);
  }
  //...
  }
  
  const latteMachine = new CafeLatteMachine(100, 100, 2, "1S");
  console.log(latteMachine.serialNumber);

 

 

coolํ•œ ์ƒ์† ๊ธฐ๋Šฅ ์ ์šฉํ•ด๋ณด๊ธฐ ์‹ค์Šต ๋!

 

<์ตœ์ข… ์ฝ”๋“œ์™€ ์ถœ๋ ฅ ๊ฒฐ๊ณผ>

- ์ตœ์ข… ์ฝ”๋“œ

//   //์ปคํ”ผ๋จธ์‹  ๋งŒ๋“ค๊ธฐ
type CoffeeCup = {
  orderShot: number;
  hasMilk: boolean;
};

interface CoffeeMaker {
  makeCoffee(shots: number): CoffeeCup;
}

//์œ„ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ํด๋ž˜์Šค์ด๋‹ค.
class CoffeMachine implements CoffeeMaker {
  private static ONE_SHOT_CAPSULE: number = 3;
  protected static ONE_CUP_WATER: number = 10;

  static makeMachine(beans: number, water: number): CoffeMachine {
    return new CoffeMachine(beans, water);
  }

  public constructor(
    protected capsule: number = 3,
    protected water: number = 10
  ) {}

  makeCoffee(orderShot: number): CoffeeCup {
    this.checkMaterials(orderShot);
    console.log("์บก์А์“ฐ๊ธฐ ์ „ " + this.capsule);
    this.useCapsule(orderShot);
    console.log("๋ฌผ์“ฐ๊ธฐ ์ „ " + this.water);
    this.useWater();
    this.heatWater();
    return this.extract(orderShot);
  }

  protected checkMaterials(orderShot: number) {
    if (this.capsule < orderShot * CoffeMachine.ONE_SHOT_CAPSULE) {
      throw new Error(`์ปคํ”ผ์บก์А ๋ถ€์กฑ!!! ํ˜„์žฌ ๋ณด์œ  ์บก์А : ${this.capsule}`);
    }
    if (this.water < CoffeMachine.ONE_CUP_WATER) {
      throw new Error(`๋ฌผ ๋ถ€์กฑ!!! ๋‚จ์€ ๋ฌผ ${this.water}`);
    }
  }
  clean(): void {
    console.log("์ปคํ”ผ๋จธ์‹  ํ–‰๊ตฌ๋Š” ์ค‘!");
  }

  private extract(shots: number): CoffeeCup {
    return {
      orderShot: shots,
      hasMilk: false,
    };
  }
  private heatWater() {
    console.log("๋ฌผ ๋“์ด๋Š” ์ค‘...");
  }

  private useCapsule(shot: number) {
    console.log("์บก์А์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.");
    this.capsule -= shot * CoffeMachine.ONE_SHOT_CAPSULE;
    console.log("์บก์А ์“ฐ๊ณ  ๋‚œ ํ›„" + this.capsule);
  }

  protected useWater() {
    console.log("๋ฌผ์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.");
    this.water -= CoffeMachine.ONE_CUP_WATER;
    console.log("๋ฌผ์“ฐ๊ณ  ๋‚œ ํ›„ " + this.water);
  }

  addCapsule(capsule: number) {
    this.capsule += capsule;
  }

  fulfillWater(water: number) {
    this.water += water;
  }

  get Water(): number {
    return this.water;
  }

  get Capsule(): number {
    return this.capsule;
  }
}

// tslint:disable-next-line: max-classes-per-file
class CafeLatteMachine extends CoffeMachine {
  protected static ONE_CUP_WATER: number = 8;
  private static ONE_CUP_Milk: number = 2;

  public constructor(
    protected capsule: number = 3,
    protected water: number = 10,
    private milk: number,
    public readonly serialNumber: string
  ) {
    super(capsule, water);
  }

  makeCoffee(orderShot: number): CoffeeCup {
    const coffee = super.makeCoffee(orderShot); //๊ธฐ๋ณธ์ ์ธ ์บก์А ์†Œ๋น„, ๋ฌผ ์†Œ๋น„, ๋ฌผ ๊ฐ€์—ด ๋‹ค ๋๋‚˜๊ณ  ์™„์„ฑ๋œ ์ปคํ”ผ๊ฐ€ ๋ฐ˜ํ™˜
    //๋ถ€๋ชจ์—์„œ ๋ฐ›์•„์˜จ ์ปคํ”ผ ์™„์„ฑํ’ˆ์— ์šฐ์œ ๋งŒ ๋ถ€์–ด์ฃผ์ž!!!
    this.steamMilk();
    this.pourMilk();
    return { ...coffee, hasMilk: true };
  }

  //์˜ค๋ฒ„๋ผ์ด๋”ฉ
  checkMaterials(orderShot: number) {
    super.checkMaterials(orderShot);
    if (this.milk < CafeLatteMachine.ONE_CUP_Milk) {
      throw new Error(`์šฐ์œ  ๋ถ€์กฑ!!! ํ˜„์žฌ ๋ณด์œ  ์šฐ์œ ๋Ÿ‰ : ${this.milk}`);
    }
  }
  useWater() {
    console.log("๋ฌผ์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.");
    this.water -= CafeLatteMachine.ONE_CUP_WATER;
    console.log("๋ฌผ์“ฐ๊ณ  ๋‚œ ํ›„ " + this.water);
  }

  private steamMilk(): void {
    console.log("์šฐ์œ  ๋ฐ์šฐ๋Š” ์ค‘...");
  }

  private pourMilk() {
    console.log("์šฐ์œ  ๋ถ“๋Š” ์ค‘...");
    this.milk -= CafeLatteMachine.ONE_CUP_Milk;
    console.log("์šฐ์œ  ๋ถ“๊ณ  ๋‚œ ๋’ค " + this.milk);
  }

  public fulfillMilk(milk: number) {
    this.milk += milk;
  }

  get Milk(): number {
    return this.milk;
  }
}

const machine = new CoffeMachine(100, 100);
const latteMachine = new CafeLatteMachine(100, 100, 2, "1S");

const latte1 = latteMachine.makeCoffee(10);
console.log("์ฒซ๋ฒˆ์งธ ๋ผ๋–ผ...๋‘๊ตฌ๋‘๊ตฌ๋‘๊ตฌ...");
console.log(latte1);

console.log("์ง€๊ธˆ ์šฐ์œ  ์ƒํƒœ๋Š”? : " + latteMachine.Milk);
console.log("์šฐ์œ ๊ฐ€ ์—†์œผ๋‹ˆ๊นŒ ์ถฉ์ „ํ•ด์ฃผ์ž");
latteMachine.fulfillMilk(2);

const latte2 = latteMachine.makeCoffee(10);
console.log("๋‘๋ฒˆ์งธ ๋ผ๋–ผ~~~");
console.log(latte2);

const latte3 = latteMachine.makeCoffee(10);
console.log("์šฐ์œ ๊ฐ€ ์—†๋Š” ์ƒํƒœ์—์„œ ์„ธ๋ฒˆ์งธ ๋ผ๋–ผ๋ฅผ ๋งŒ๋“ค๋ ค๊ณ  ํ•˜๋ฉด~~?");
console.log(latte3);
console.log(latteMachine.serialNumber);

- ์ถœ๋ ฅ ๊ฒฐ๊ณผ

์บก์А์“ฐ๊ธฐ ์ „ 100
์บก์А์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
์บก์А ์“ฐ๊ณ  ๋‚œ ํ›„70
๋ฌผ์“ฐ๊ธฐ ์ „ 100
๋ฌผ์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
๋ฌผ์“ฐ๊ณ  ๋‚œ ํ›„ 92
๋ฌผ ๋“์ด๋Š” ์ค‘...
์šฐ์œ  ๋ฐ์šฐ๋Š” ์ค‘...
์šฐ์œ  ๋ถ“๋Š” ์ค‘...
์šฐ์œ  ๋ถ“๊ณ  ๋‚œ ๋’ค 0
์ฒซ๋ฒˆ์งธ ๋ผ๋–ผ...๋‘๊ตฌ๋‘๊ตฌ๋‘๊ตฌ...
{ orderShot: 10, hasMilk: true }
์ง€๊ธˆ ์šฐ์œ  ์ƒํƒœ๋Š”? : 0
์šฐ์œ ๊ฐ€ ์—†์œผ๋‹ˆ๊นŒ ์ถฉ์ „ํ•ด์ฃผ์ž
์บก์А์“ฐ๊ธฐ ์ „ 70
์บก์А์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
์บก์А ์“ฐ๊ณ  ๋‚œ ํ›„40
๋ฌผ์“ฐ๊ธฐ ์ „ 92
๋ฌผ์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
๋ฌผ์“ฐ๊ณ  ๋‚œ ํ›„ 84
๋ฌผ ๋“์ด๋Š” ์ค‘...
์šฐ์œ  ๋ฐ์šฐ๋Š” ์ค‘...
์šฐ์œ  ๋ถ“๋Š” ์ค‘...
์šฐ์œ  ๋ถ“๊ณ  ๋‚œ ๋’ค 0
๋‘๋ฒˆ์งธ ๋ผ๋–ผ~~~
{ orderShot: 10, hasMilk: true }
/Users/๊ฒฝ๋กœ๋ช…/Desktop/ํ”„๋กœ์ ํŠธ๋ช…/dist/index.js:87
            throw new Error(`์šฐ์œ  ๋ถ€์กฑ!!! ํ˜„์žฌ ๋ณด์œ  ์šฐ์œ ๋Ÿ‰ : ${this.milk}`);
            ^

Error: ์šฐ์œ  ๋ถ€์กฑ!!! ํ˜„์žฌ ๋ณด์œ  ์šฐ์œ ๋Ÿ‰ : 0
Node.js v17.7.1

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

728x90

'TypeScript > ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ TypeScript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ๊ฐ์ฒด์ง€ํ–ฅ์˜ ๊ฝƒ composition(๋ถ€์ œ: ์ƒ์†์˜ ๋ฌธ์ œ์ )  (0) 2022.04.22
[ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ๋‹คํ˜•์„ฑ(polymorphism)์ด ๋ญ๊ฐ€ ์ข‹์€ ๊ฒƒ์ผ๊นŒ?  (0) 2022.04.22
[ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] OOP ๋‹คํ˜•์„ฑ, ์ƒ์† ๊ฐœ๋… ํ™•์‹คํ•˜๊ฒŒ ์ •๋ฆฌ  (0) 2022.04.21
[ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ์ œ๋„ค๋ฆญ ํƒ€์ž… ๋ณ„์นญ  (0) 2022.04.21
[ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] 2๊ฐœ ์ด์ƒ์˜ ์ œ๋„ค๋ฆญ ์ ์šฉํ•ด๋ณด๊ธฐ(์ถ”๋ก , ๋ช…์‹œ, ํ”„๋กœ๋ฏธ์Šค ์ œ๋„ค๋ฆญ๊นŒ์ง€)  (0) 2022.04.21
    'TypeScript/ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ TypeScript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • [ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ๊ฐ์ฒด์ง€ํ–ฅ์˜ ๊ฝƒ composition(๋ถ€์ œ: ์ƒ์†์˜ ๋ฌธ์ œ์ )
    • [ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ๋‹คํ˜•์„ฑ(polymorphism)์ด ๋ญ๊ฐ€ ์ข‹์€ ๊ฒƒ์ผ๊นŒ?
    • [ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] OOP ๋‹คํ˜•์„ฑ, ์ƒ์† ๊ฐœ๋… ํ™•์‹คํ•˜๊ฒŒ ์ •๋ฆฌ
    • [ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ์ œ๋„ค๋ฆญ ํƒ€์ž… ๋ณ„์นญ
    Rainbow๐ŸŒˆCoder
    Rainbow๐ŸŒˆCoder
    ๋ชฐ๋ผ๋„ ๊ฒฐ๊ตญ์€ ์•„๋Š” ๊ฐœ๋ฐœ์ž, ๊ทธ๋Ÿฐ ์‚ฌ๋žŒ์ด ๋˜๊ธฐ ์œ„ํ•œ ๋งค์ผ์˜ ํ•œ๊ฑธ์Œ

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”