[TS] ๐Ÿฅฆtypescript์˜ class ์‚ฌ์šฉ๋ฒ•

TATAยท2023๋…„ 3์›” 27์ผ
0

TypeScript

๋ชฉ๋ก ๋ณด๊ธฐ
2/4

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ์˜ class ์‚ฌ์šฉ๋ฒ•๊ณผ ๋น„์Šทํ•˜์ง€๋งŒ,
์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ์˜ ์€๋‹‰ํ™”#์™€ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์—์„œ์˜ ์€๋‹‰ํ™”private๋Š” ๋‹ค๋ฅด๊ณ ,
์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—๋Š” ์—†์ง€๋งŒ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” ์žˆ๋Š” ๊ฒƒ๋“ค์ด ์žˆ๋‹ค.

๊ทธ๋ž˜์„œ ์ •๋ฆฌํ•ด ๋ณด๋Š” typescript์—์„œ์˜ class ์‚ฌ์šฉ๋ฒ•๐Ÿ’จ

๐Ÿฅฆ ๊ธฐ๋ณธ ํ˜•ํƒœ ์˜ˆ์‹œ

class Person {
  name: string;
  age: number;

  constructor(name: string, age:number) {
    this.name = name
    this.age = age
  }

  introduce(): string {
    return `์ œ ์ด๋ฆ„์€ ${this.name}์ด๊ณ , ๋‚˜์ด๋Š” ${this.age}์‚ด ์ž…๋‹ˆ๋‹ค.`
  }
}

const a = new Person('taehyung', 29);
console.log(a.introduce()); // "์ œ ์ด๋ฆ„์€ taehyung์ด๊ณ , ๋‚˜์ด๋Š” 29์‚ด ์ž…๋‹ˆ๋‹ค."

๐Ÿฅฆ extends

ํ™•์žฅ (๋˜๋Š” ์ƒ์†์ด๋ผ ๋ถˆ๋ฆผ)

class Person {
  name: string;
  age: number;

  constructor(name: string, age:number) {
    this.name = name;
    this.age = age;
  }

  introduce(): string {
    return `์ œ ์ด๋ฆ„์€ ${this.name}์ด๊ณ , ๋‚˜์ด๋Š” ${this.age}์ž…๋‹ˆ๋‹ค.`
  }
}

class Character extends Person {
  nickname: string;

  constructor(name: string, age: number, nickname: string) {
    super(name, age);
    this.nickname = nickname;
  }

  introduce2(): string {
    // ์ด๋ ‡๊ฒŒ super๋กœ ๋ถ€๋ชจ์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
    return `${super.introduce()} ๋‹‰๋„ค์ž„์€ ${this.nickname}์ž…๋‹ˆ๋‹ค.`
  }
}

const b = new Character('taehyung', 29, 'TATA');
console.log(b.introduce2()); // "์ œ ์ด๋ฆ„์€ taehyung์ด๊ณ , ๋‚˜์ด๋Š” 29์ž…๋‹ˆ๋‹ค. ๋‹‰๋„ค์ž„์€ TATA์ž…๋‹ˆ๋‹ค."

๐Ÿฅฆ getter / setter

get : ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ
set : ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ์„ค์ •ํ•˜๋Š” ๋ฉ”์„œ๋“œ

class Person {
  name: string;
  // private์„ ์‚ฌ์šฉํ•  ๋• _(์–ธ๋”๋ฐ”)๋ฅผ ๋ถ™์ด๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.
  // private์€ .(์ )์œผ๋กœ ์ ‘๊ทผ์ด ๋ถˆ๊ฐ€ํ•˜๋‹ค.
  private _age: number;

  constructor(name: string, age:number) {
    this.name = name;
    this._age = age;
  }

  // ์ฐธ๊ณ ) getter๋Š” setter๊ฐ€ ์—†์œผ๋ฉด ๊ทธ๋ƒฅ ์ฝ๊ธฐ ์ „์šฉ์ด ๋จ
  get age() {
    if (this._age <= 0 ) {
      return `๋‚˜์ด๋ฅผ ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.`
    }
    return `๋‚˜์ด๋Š” ${this._age}์‚ด ์ž…๋‹ˆ๋‹ค.`
  }

  set changeAge(age: number) {
    this._age = age
  }

  introduce(): string {
    return `์ œ ์ด๋ฆ„์€ ${this.name}์ด๊ณ , ๋‚˜์ด๋Š” ${this._age}์ž…๋‹ˆ๋‹ค.`
  }
}

const a = new Person('TATA', -1);
console.log(a.age) // "๋‚˜์ด๋ฅผ ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."

a.changeAge = 7
console.log(a.age) // "๋‚˜์ด๋Š” 7์‚ด ์ž…๋‹ˆ๋‹ค."

๐Ÿฅฆ ์ ‘๊ทผ ์ œ์–ด์ž

public : ํด๋ž˜์Šค์˜ ๋‚ด๋ถ€๋‚˜ ์™ธ๋ถ€, ์–ด๋””์„œ๋“  ์ ‘๊ทผ ๊ฐ€๋Šฅ
protected : ํ•ด๋‹นํ•˜๋Š” ํด๋ž˜์Šค์™€ ์ž์‹ ํด๋ž˜์Šค์—์„œ๋งŒ ์ ‘๊ทผ ๊ฐ€๋Šฅ
private : ํ•ด๋‹นํ•˜๋Š” ํด๋ž˜์Šค์—์„œ๋งŒ ์ ‘๊ทผ ๊ฐ€๋Šฅ

class User {
  public nickname: string; // public์€ ์ƒ๋žต ๊ฐ€๋Šฅํ•จ. nickname: string์ด๋ž‘ ๊ฐ™์Œ.
  protected name: string; // ๋ณธ์ธ๊ณผ ์ž์‹ ํด๋ž˜์Šค์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
  private _job: string; // ๋ณธ์ธ ํด๋ž˜์Šค์—์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

  constructor(nickname: string, name: string, job: string) {
    this.nickname = nickname;
    this.name = name;
    this._job = job;
  }

  // ๋ฉ”์„œ๋“œ ์•ž์—๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
  protected introduceJob() {
    return `์ œ ์ง์—…์€ ${this._job} ์ž…๋‹ˆ๋‹ค.`
  }

  // private์ด๋ผ์„œ ์ž์‹ ํด๋ž˜์Šค์—๊ฒŒ ๋ชป ๋„˜๊ฒจ ์คŒ
  private introduceName() {
    return `์ œ ์ด๋ฆ„์€ ${this.name} ์ž…๋‹ˆ๋‹ค.`
  }
}

class Level extends User {
  level: number;

  constructor(nickname: string, name: string, job: string, level: number) {
    super(nickname, name, job);
    this.level = level;
  }

  gameIntroduce() {
    return `${super.introduceJob()} ๋ ˆ๋ฒจ์€ ${this.level} ์ž…๋‹ˆ๋‹ค.`
  }
}

const a = new Level('TATA', 'taehyung', '๋งˆ๋ฒ•์‚ฌ', 99)
console.log(a.gameIntroduce()) // "์ œ ์ง์—…์€ ๋งˆ๋ฒ•์‚ฌ ์ž…๋‹ˆ๋‹ค. ๋ ˆ๋ฒจ์€ 99 ์ž…๋‹ˆ๋‹ค."

๐Ÿฅฆ static

new๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ธ์Šคํ„ดํŠธ๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๊ณ ๋„
ํด๋ž˜์Šค์˜ ์†์„ฑ๊ณผ ๋ฉ”์„œ๋“œ์— ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

class User {
  static nickname = "TATA"

  static hi() {
    return `hello ${this.nickname}`
  }
}

console.log(User.nickname) // "TATA"
console.log(User.hi()) // "hello TATA"

๐Ÿฅฆ ์ถ”์ƒ ํด๋ž˜์Šค

abstract

abstract์œผ๋กœ ์„ ์–ธํ•œ ํด๋ž˜์Šค๋Š”
new๋กœ ์ง์ ‘ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์—†๋‹ค.

extends๋กœ ๋งŒ๋“  ์ž์‹ ํด๋ž˜์Šค์—๊ฒŒ
์ธ์Šคํ„ด์Šคํ™”ํ•˜๋„๋ก ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค.

abstract class Game {
  // ์ž์‹ ํด๋ž˜์Šค์—์„œ ๋ฌด์กฐ๊ฑด ์ด ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•จ
  abstract test1(): string

  // ๊ทธ๋ƒฅ ์ž‘์„ฑํ•œ ๋ฉ”์„œ๋“œ
  test2() {
    return "hello world!"
  }
}

class User extends Game {
  test1() {
    return "wow"
  }
}

const a = new User()
console.log(a.test1()) // "wow"

โœš ๋ค

์•„๋ž˜์ฒ˜๋Ÿผ ์ ์œผ๋ฉด ๊ฐ€๋…์„ฑ์€ ์ข‹์ง€ ์•Š์ง€๋งŒ,
์—ฌ๋Ÿฌ๋ฒˆ ์ ๋Š” ๊ท€์ฐฎ์Œ์„ ์—†์• ์ค„ ์ˆ˜ ์žˆ๋‹ค.

class User {
  constructor(public nickname: string, protected name: string, private _job: string) {
  }

  introduceJob() {
    return `์ œ ์ง์—…์€ ${this._job} ์ž…๋‹ˆ๋‹ค.`
  }
}

const a = new User('TATA', 'taehyung', '๋งˆ๋ฒ•์‚ฌ');
console.log(a.introduceJob()) // "์ œ ์ง์—…์€ ๋งˆ๋ฒ•์‚ฌ ์ž…๋‹ˆ๋‹ค." 



๐Ÿงชtypescript ๊ธฐ๋ณธ ํƒ€์ž… ๋ณด๋Ÿฌ๊ฐ€๊ธฐ
๐Ÿ‘‰ TypeScript - Playground

profile
๐Ÿพ

0๊ฐœ์˜ ๋Œ“๊ธ€