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

[ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ์ ˆ์ฐจ์ง€ํ–ฅ์  ์ปคํ”ผ๋จธ์‹  vs ๊ฐ์ฒด์ง€ํ–ฅ์  ์ปคํ”ผ๋จธ์‹ 

2022. 4. 18. 11:07
728x90

<์ ˆ์ฐจ์ง€ํ–ฅ์  ์ปคํ”ผ๋จธ์‹  ์„ค๊ณ„>

  type CoffeeCup = {
    shots: number;
    hasMilk: boolean;
  };
  const BEANS_GRAMM_PER_SHOT: number = 7;
  let coffeeBeans: number = 0;
  function makeCoffee(shots: number): CoffeeCup {
    if (coffeeBeans < shots * BEANS_GRAMM_PER_SHOT) {
      throw new Error("Not enough coffee beans!");
    }
    coffeeBeans -= shots * BEANS_GRAMM_PER_SHOT;
    return {
      shots,
      hasMilk: false,
    };
  }

  function addCoffeeBeans(shots: number) {
    coffeeBeans = coffeeBeans += BEANS_GRAMM_PER_SHOT * shots;
  }

  addCoffeeBeans(2);
  let coffee = makeCoffee(2);
  console.log(coffee);
  console.log("๋‚จ์€ ์ปคํ”ผ์ฝฉ: " + coffeeBeans + "๊ฐœ");

 

 

<๊ฐ์ฒด์ง€ํ–ฅ์  ์ปคํ”ผ๋จธ์‹  ์„ค๊ณ„>

- ์ปคํ”ผ๋จธ์‹  ํด๋ž˜์Šค ํ•„์š” (ํ”„๋กœํผํ‹ฐ: ์ปคํ”ผ๋นˆ์ฆˆ, ํ•จ์ˆ˜: makeCoffee)

- ํด๋ž˜์Šค ์•ˆ ํ”„๋กœํผํ‹ฐ์—๋Š” let, const, function ํ‚ค์›Œ๋“œ ๋ถˆํ•„์š”

- ํด๋ž˜์Šค ๋ฐ”๋กœ ์•ˆ์— ์žˆ๋Š” ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์— ์ ‘๊ทผํ•  ๋•Œ๋Š” this.

- ํด๋ž˜์Šค์— construtor๋Š” ์–ธ์ œ๋‚˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ํด๋ž˜์Šค๋กœ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค ๋•Œ, ํ•ญ์ƒ ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.

const coffeeMaker = new coffeeMachine(); //์ด ๊ด„ํ˜ธ๊ฐ€ ๋ฐ”๋กœ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ.

 <์˜ˆ์‹œ1>

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

  class coffeeMachine {
    BEANS_GRAMM_PER_SHOT: number = 7;
    coffeeBeans: number = 0;

    constructor() {
      //
    }

    makeCoffee(shots: number): CoffeeCup {
      if (this.coffeeBeans < shots * this.BEANS_GRAMM_PER_SHOT) {
        throw new Error("Not enough coffee beans!");
      }
      this.coffeeBeans -= shots * this.BEANS_GRAMM_PER_SHOT;
      return {
        shots,
        hasMilk: false,
      };
    }

    addCoffeeBeans(shots: number) {
      this.coffeeBeans += this.BEANS_GRAMM_PER_SHOT * shots;
    }
  }

  const coffeeMaker = new coffeeMachine();
  console.log(coffeeMaker); //coffeeMachine { BEANS_GRAMM_PER_SHOT: 7, coffeeBeans: 24 }
  coffeeMaker.addCoffeeBeans(2);
  let order1 = coffeeMaker.makeCoffee(2);
  console.log(order1);    //{ shots: 2, hasMilk: false }
  let order2 = coffeeMaker.makeCoffee(2);
  console.log(order2);    //Error: Not enough coffee beans!

 

<์˜ˆ์‹œ2>

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

  class coffeeMachine {
    BEANS_GRAMM_PER_SHOT: number = 7;
    coffeeBeans: number = 0;

    constructor(coffeeBeans: number) {
      this.coffeeBeans = coffeeBeans;
    }

    makeCoffee(shots: number): CoffeeCup {
      if (this.coffeeBeans < shots * this.BEANS_GRAMM_PER_SHOT) {
        throw new Error("Not enough coffee beans!");
      }
      this.coffeeBeans -= shots * this.BEANS_GRAMM_PER_SHOT;
      return {
        shots,
        hasMilk: false,
      };
    }

    addCoffeeBeans(shots: number) {
      this.coffeeBeans += this.BEANS_GRAMM_PER_SHOT * shots;
    }
  }

  const coffeeMaker = new coffeeMachine(24);
  console.log(coffeeMaker); //coffeeMachine { BEANS_GRAMM_PER_SHOT: 7, coffeeBeans: 24 }
  let order1 = coffeeMaker.makeCoffee(2);
  console.log(order1); //{ shots: 2, hasMilk: false }
  let order2 = coffeeMaker.makeCoffee(2);
  console.log(order2); //Error: Not enough coffee beans!

 

๊ทธ๋Ÿฐ๋ฐ, ์œ„์˜ ์˜ˆ์‹œ๋“ค์— ๋”ฐ๋ฅด๋ฉด

    BEANS_GRAMM_PER_SHOT: number = 7; //ํด๋ž˜์Šค ๋‚ด์—์„œ ๋ณ€์น˜์•Š์„ ๊ณ ์ • ๊ฐ’

ํด๋ž˜์Šค ๋‚ด๋ถ€์— ๋ณ€์น˜์•Š์„ ๊ณ ์ •๊ฐ’์ด ์žˆ๋Š”๋ฐ, ์ด๋Ÿฌํ•œ ๊ฒƒ๋“ค์„ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋กœ ์ž‘์„ฑํ•˜๊ฒŒ ๋˜๋ฉด

ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•ด์„œ ์ฐ์–ด๋‚ด๋Š” ์˜ค๋ธŒ์ ํŠธ๋งˆ๋‹ค ์—ฌ๋Ÿฌ ํด๋ž˜์Šค๋ฅผ ์ฐ์–ด๋‚ผ ๋•Œ๋งˆ๋‹ค ๋˜‘๊ฐ™์€ ๋ฐ์ดํ„ฐ๊ฐ€ ์ค‘๋ณตํ•ด์„œ ๋“ค์–ด๊ฐ€๊ฒŒ ๋˜๋Š” ์…ˆ์ด๋‹ค.

<์˜ˆ์‹œ>

  console.log(coffeeMaker1); //coffeeMachine { BEANS_GRAMM_PER_SHOT: 7, coffeeBeans: 24 }
  console.log(coffeeMaker2); //coffeeMachine { BEANS_GRAMM_PER_SHOT: 7, coffeeBeans: 32 }
  console.log(coffeeMaker3); //coffeeMachine { BEANS_GRAMM_PER_SHOT: 7, coffeeBeans: 7 }

  ์ด์ฒ˜๋Ÿผ ํด๋ž˜์Šค์—์„œ ํ•œ๋ฒˆ ์ •์˜๋˜์–ด ์ง€๊ณ , ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•œ ์˜ค๋ธŒ์ ํŠธ ์‚ฌ์ด์—์„œ ๋‹ค... ๊ณต์œ ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋Š” ํ”„๋กœํผํ‹ฐ๋Š” ํ‰๋ฒ”ํ•œ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋กœ ๋‘๊ฒŒ๋˜๋ฉด ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์ฐ์–ด๋‚ผ ๋•Œ๋งˆ๋‹ค ์ค‘๋ณต์ ์œผ๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ์ƒ์„ฑ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฉ”๋ชจ๋ฆฌ ๋‚ญ๋น„๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

์ด๋Ÿด ๋•Œ, static ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์ด๊ฒŒ ๋˜๋ฉด class level๋กœ ์ง€์ •๋œ๋‹ค. (ํด๋ž˜์Šค level์ด๋ผ๊ณ  ํ•˜๋Š” ๊ฒƒ์€ ํด๋ž˜์Šค์™€ ์—ฐ๊ฒฐ์ด ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์˜ค๋ธŒ์ ํŠธ๋งˆ๋‹ค ์ƒ์„ฑ๋˜์ง€ ์•Š๋Š”๋‹ค~) ๊ทธ๋ฆฌ๊ณ  ์ด๊ฒƒ๋“ค์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” this.(์˜ค๋ธŒ์ ํŠธ ๋ ˆ๋ฒจ) ์ด ์•„๋‹ˆ๋ผ ํด๋ž˜์Šค ๋ ˆ๋ฒจ์— ์žˆ๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— this.๋ฅผ ์“ฐ์ง€ ์•Š๊ณ  ๋Œ€์‹ ์— ํด๋ž˜์Šค์ด๋ฆ„. ์„ ์“ด๋‹ค.

(static ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ€์ด์ง€ ์•Š์œผ๋ฉด inctance(object) level๋กœ ์ง€์ •๋œ๋‹ค.)

 

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

  class coffeeMachine {
    static BEANS_GRAMM_PER_SHOT: number = 7;
    coffeeBeans: number = 0;

    constructor(coffeeBeans: number) {
      this.coffeeBeans = coffeeBeans;
    }

    makeCoffee(shots: number): CoffeeCup {
      if (this.coffeeBeans < shots * coffeeMachine.BEANS_GRAMM_PER_SHOT) {
        throw new Error("Not enough coffee beans!");
      }
      this.coffeeBeans -= shots * coffeeMachine.BEANS_GRAMM_PER_SHOT;
      return {
        shots,
        hasMilk: false,
      };
    }

    addCoffeeBeans(shots: number) {
      this.coffeeBeans += coffeeMachine.BEANS_GRAMM_PER_SHOT * shots;
    }
  }

  const coffeeMaker1 = new coffeeMachine(24);
  const coffeeMaker2 = new coffeeMachine(32);
  const coffeeMaker3 = new coffeeMachine(7);

  console.log(coffeeMaker1); //coffeeMachine { coffeeBeans: 24 }
  console.log(coffeeMaker2); //coffeeMachine { coffeeBeans: 32 }
  console.log(coffeeMaker3); //coffeeMachine { coffeeBeans: 7 }

์ฆ‰, coffeeMachine.BEANS_GRAMM_PER_SHOT ๋ผ๊ณ  ์ง€์ •ํ•ด์คŒ์œผ๋กœ์จ,

ํด๋ž˜์Šค๋‹จ์— ์žˆ๋Š” BEANS_GRAMM_PER_SHOT์— ์ ‘๊ทผํ•˜๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

 

  console.log(coffeeMaker1); //coffeeMachine { coffeeBeans: 24 }
  console.log(coffeeMaker2); //coffeeMachine { coffeeBeans: 32 }
  console.log(coffeeMaker3); //coffeeMachine { coffeeBeans: 7 } 

 

๊ทธ๋ž˜์„œ ์ฝ˜์†”๋กœ๊ทธ๋ฅผ ์‚ดํŽด๋ณด๋ฉด BEANS_GRAMM_PER_SHOT: 7๋ผ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ๋”์ด์ƒ ์ค‘๋ณต ์ƒ์„ฑ๋˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

 


์ด์ฒ˜๋Ÿผ ํด๋ž˜์Šค๋Š” ๊ด€๋ จ๋œ ์†์„ฑ๋“ค๊ณผ ํ•จ์ˆ˜๋“ค์„ ๋ฌถ์–ด์„œ ์–ด๋–ค ๋ชจ์–‘์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ๋  ๊ฒƒ์ธ์ง€ ์ •์˜ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

์ด ์ •์˜๋œ ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด ์ธ์Šคํ„ด์Šค(๊ฐ์ฒด)๋ฅผ ์ฐ์–ด๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

์ด๋•Œ ์˜ค๋ธŒ์ ํŠธ๋งˆ๋‹ค ๋‹ฌ๋ผ์งˆ ๋ณ€์ˆ˜๋“ค์€ ์˜ค๋ธŒ์ ํŠธ๋ ˆ๋ฒจ๋กœ,

๊ฐ™์€ ํด๋ž˜์Šค ์ถœ์‹ ์ด๋ฉด ๋ณ€์น˜์•Š๊ณ  ๊ณตํ†ต๋  ๋ณ€์ˆ˜๋“ค์€ ํด๋ž˜์Šค๋ ˆ๋ฒจ(static ํ‚ค์›Œ๋“œ ๋ถ™์—ฌ์ฃผ๊ธฐ)๋กœ ์„ค์ •ํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค. 

์ด๋Ÿฌํ•œ static ํ‚ค์›Œ๋“œ๋Š” ๋ฉค๋ฒ„ ๋ณ€์ˆ˜ ๋ฟ๋งŒ์ด ์•„๋‹ˆ๋ผ ํ•จ์ˆ˜์—์„œ๋„ ์ ์šฉ๋œ๋‹ค.

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

  class coffeeMachine {
    static BEANS_GRAMM_PER_SHOT: number = 7;
    coffeeBeans: number = 0;

    constructor(coffeeBeans: number) {
      this.coffeeBeans = coffeeBeans;
    }

    static makeMachine(coffeeBeans: number): coffeeMachine {
      return new coffeeMachine(coffeeBeans);
    }

    makeCoffee(shots: number): CoffeeCup {
      if (this.coffeeBeans < shots * coffeeMachine.BEANS_GRAMM_PER_SHOT) {
        throw new Error("Not enough coffee beans!");
      }
      this.coffeeBeans -= shots * coffeeMachine.BEANS_GRAMM_PER_SHOT;
      return {
        shots,
        hasMilk: false,
      };
    }

    addCoffeeBeans(shots: number) {
      this.coffeeBeans += coffeeMachine.BEANS_GRAMM_PER_SHOT * shots;
    }
  }

  const coffeeMaker1 = new coffeeMachine(24);
  const coffeeMaker2 = new coffeeMachine(32);
  const coffeeMaker3 = new coffeeMachine(7);

  console.log(coffeeMaker1); //coffeeMachine { coffeeBeans: 24 }
  console.log(coffeeMaker2); //coffeeMachine { coffeeBeans: 32 }
  console.log(coffeeMaker3); //coffeeMachine { coffeeBeans: 7 }

  console.log(coffeeMachine.makeMachine(2)); //coffeeMachine { coffeeBeans: 2 }

 

<๋‹๋ณด๊ธฐ>

constructor์„ ํ˜ธ์ถœํ•˜์ง€ ์•Š๊ณ  ์ปคํ”ผ๊ธฐ๊ณ„๋ฅผ ๋งŒ๋“ค๊ณ  ์‹ถ๋‹ค๋ฉด,

    static makeMachine(coffeeBeans: number): coffeeMachine {
      return new coffeeMachine(coffeeBeans);
    }
    
    console.log(coffeeMachine.makeMachine(2)); //coffeeMachine { coffeeBeans: 2 }

์œ„์™€ ๊ฐ™์€ ํ•จ์ˆ˜๋“ค์€ ํด๋ž˜์Šค ๋‚ด๋ถ€์˜ ์–ด๋– ํ•œ ์†์„ฑ๊ฐ’๋„ ํ•„์š”์น˜์•Š๊ธฐ ๋•Œ๋ฌธ์— static์„ ๋ถ™์—ฌ์ฃผ๋ฉด ๋œ๋‹ค.

๊ทธ๋Ÿฌ๋ฉด ์™ธ๋ถ€์—์„œ๋„ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๊ณ  

coffeeMachine.makeMachine(2)

์ด์ฒ˜๋Ÿผ ํด๋ž˜์Šค ๋‹จ์—์„œ ํ˜ธ์ถœํ•˜์—ฌ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ปคํ”ผ๋จธ์‹ ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค. 

static ํ‚ค์›Œ๋“œ๊ฐ€ ์—†์œผ๋ฉด ๋‹น์—ฐํžˆ ํด๋ž˜์Šค ๋‹จ์—์„œ ํ˜ธ์ถœํ•  ์—†๊ณ  ์ฐ์–ด ๋งŒ๋“  ์˜ค๋ธŒ์ ํŠธ ๋‹จ์—์„œ ํ˜ธ์ถœํ•ด์•ผํ•œ๋‹ค.

 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” Math ํด๋ž˜์Šค๊ฐ€ ์ข‹์€ ์˜ˆ์‹œ์ด๋‹ค.

const math = new Math(); ๋ผ๊ณ  ๊ตณ์ด ์ฐ์–ด๋‚ด์ง€ ์•Š๊ณ 

๋ฐ”๋กœ

Maht.abs๋ผ๊ณ  ๋ถ€๋ฅผ ์ˆ˜ ์žˆ๋Š” ์ด์œ ๋Š” ํด๋ž˜์Šค ๋‹จ(๋ ˆ๋ฒจ)์ด๊ธฐ ๋•Œ๋ฌธ์ด๋ผ๋Š” ๊ฒƒ์„ ์œ ์ถ”ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

<์ตœ์ข… ์ฝ”๋“œ>

๊ทธ๋Ÿฌ๋‚˜ ์ด ์ฝ”๋“œ์ƒ์˜ ํด๋ž˜์Šค๋Š” ์•„์ง 

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

  class coffeeMachine {
    static BEANS_GRAMM_PER_SHOT: number = 7;
    coffeeBeans: number = 0;

    constructor(coffeeBeans: number) {
      this.coffeeBeans = coffeeBeans;
    }

    static makeMachine(coffeeBeans: number): coffeeMachine {
      return new coffeeMachine(coffeeBeans);
    }

    makeCoffee(shots: number): CoffeeCup {
      if (this.coffeeBeans < shots * coffeeMachine.BEANS_GRAMM_PER_SHOT) {
        throw new Error("Not enough coffee beans!");
      }
      this.coffeeBeans -= shots * coffeeMachine.BEANS_GRAMM_PER_SHOT;
      return {
        shots,
        hasMilk: false,
      };
    }

    addCoffeeBeans(shots: number) {
      this.coffeeBeans += coffeeMachine.BEANS_GRAMM_PER_SHOT * shots;
    }
  }

  const coffeeMaker1 = new coffeeMachine(24);
  const coffeeMaker2 = new coffeeMachine(32);
  const coffeeMaker3 = new coffeeMachine(7);

  console.log(coffeeMaker1); //coffeeMachine { coffeeBeans: 24 }
  console.log(coffeeMaker2); //coffeeMachine { coffeeBeans: 32 }
  console.log(coffeeMaker3); //coffeeMachine { coffeeBeans: 7 }

  const coffeeMaker4 = coffeeMachine.makeMachine(2);

  console.log(coffeeMaker4); //coffeeMachine { coffeeBeans: 2 }

์บก์Аํ™”๊ฐ€ ๋˜์–ด ์žˆ์ง€ ์•Š๋‹ค.

  const coffeeMaker1 = new coffeeMachine(24);

  coffeeMaker1.coffeeBeans = -1000;

  console.log(coffeeMaker1); //coffeeMachine { coffeeBeans: -1000 }

์œ„์— ๋ณด์ด๋‹ค์‹ถ์ด, ํด๋ž˜์Šค ์™ธ๋ถ€์— ์ฐ์–ด๋‚ธ ์˜ค๋ธŒ์ ํŠธ ๋‹จ์—์„œ ํ•ด๋‹น ์˜ค๋ธŒ์ ํŠธ ๋‚ด๋ถ€ ๋ณ€์ˆ˜๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

-1000์ด๋ผ๋Š” ์ด์ƒํ•œ ๊ฐ’์„ ๋„ฃ์–ด๋„ ์ œ์•ฝ์‚ฌํ•ญ์ด ์—†์–ด์„œ ๋ณ€๊ฒฝ์ด ๊ทธ๋ž˜๋„ ์ ์šฉ๋œ ๋ชจ์Šต.

์ด์ฒ˜๋Ÿผ ์™ธ๋ถ€์—์„œ ๋‚˜์˜ ์˜ค๋ธŒ์ ํŠธ ์ƒํƒœ๋ฅผ ์œ ํšจํ•˜์ง€ ์•Š์€ ์ƒํƒœ๋กœ ๋งŒ๋“ค์–ด๋ฒ„๋ฆฐ ์œ„ํ—˜ํ•œ ์ƒํ™ฉ์ด๋‹ค.

๊ทธ๋Ÿฌ๋ฏ€๋กœ, ์บก์Аํ™”๋ฅผ ํ†ตํ•ด์„œ ์™ธ๋ถ€์—์„œ ์ƒˆ๋กœ ์“ฐ๋Š” ๊ฒƒ์„ ๋ง‰์•„์ฃผ์–ด์•ผ๋งŒ ํ•œ๋‹ค.

//private,

//private,

//protected

๋“ฑ์˜ ํ‚ค์›Œ๋“œ๋“ค์„ ์ด์šฉํ•ด์„œ ๋‹ค์–‘ํ•œ ๋ ˆ๋ฒจ์˜ ์ •๋ณด๋“ค์„ ์€๋‹‰ํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ด๋Ÿฌํ•œ ์บก์Аํ™”, ์€๋‹‰ํ™” ์ฒ˜๋ฆฌ๋Š” ๋‹ค์Œ ํฌ์ŠคํŒ…์—์„œ ๋‹ค๋ฃจ๋„๋ก ํ•œ๋‹ค.

728x90

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

[ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] class์˜ getter์™€ setter  (0) 2022.04.18
[ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ๊ฐ์ฒด์ง€ํ–ฅ์  ์ปคํ”ผ๋จธ์‹  ๋งŒ๋“ค๊ธฐ : encapsulation  (0) 2022.04.18
[ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ „์šฉ ์˜ˆ์‹œ] ๊ฐ์ฒด์ง€ํ–ฅ 4๊ฐ€์ง€ ์›์น™  (0) 2022.04.17
ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ๋กœ๋”ฉ ์ƒํƒœ ํ‘œ์‹œ ์˜ˆ์ œ  (0) 2022.04.17
ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ขŒํ‘œ ์ด๋™ ์˜ˆ์ œ  (0) 2022.04.17
    'TypeScript/ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ TypeScript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • [ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] class์˜ getter์™€ setter
    • [ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ๊ฐ์ฒด์ง€ํ–ฅ์  ์ปคํ”ผ๋จธ์‹  ๋งŒ๋“ค๊ธฐ : encapsulation
    • [ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ „์šฉ ์˜ˆ์‹œ] ๊ฐ์ฒด์ง€ํ–ฅ 4๊ฐ€์ง€ ์›์น™
    • ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ๋กœ๋”ฉ ์ƒํƒœ ํ‘œ์‹œ ์˜ˆ์ œ
    Rainbow๐ŸŒˆCoder
    Rainbow๐ŸŒˆCoder
    ๋ชฐ๋ผ๋„ ๊ฒฐ๊ตญ์€ ์•„๋Š” ๊ฐœ๋ฐœ์ž, ๊ทธ๋Ÿฐ ์‚ฌ๋žŒ์ด ๋˜๊ธฐ ์œ„ํ•œ ๋งค์ผ์˜ ํ•œ๊ฑธ์Œ

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