본문 바로가기

Study Output for Myself/TypeScript

[TS]속성접근자

속성접근자(Property Accessor)

class내의 속성에 접근할 수 있는 범위를 설정해주는 역할

public

class내, 자식class, 완전 외부에서도 전부 접근 가능한 접근자
interface에서의 모든 속성/메소드는 public 속성접근자를 갖는다. interface를 구현한(implemented) class에서 속성접근자를 다른 접근자로 바꿀 수 없다.

private

오로지 자기 class 내에서만 접근 가능한 접근자

protected

자기 자신의 class와 자식 class 내에서 접근 가능한 접근자

abstract

abstract 가 붙으면 자식 class에서 정의해줘야하는 의무가 생긴다.
class 앞에도 abstract가 붙어야 한다.

abstract class User{
  constructor(name: string){
    this.name = name
  }

  abstract run():void
}

class Dahye extends User{
  constructor(){
      super('dahye')
  }

  run(){
    console.log(`${this.name} can run`)
  }

'Study Output for Myself > TypeScript' 카테고리의 다른 글

[Typescript] Utility Types  (0) 2023.04.28
[TS] type narrowing, discriminated union  (0) 2023.03.28
[TS] Generic(제네릭)  (0) 2022.07.05
[TS]인터페이스(interface)  (0) 2022.07.02
[TS]타입 알리아스(type alias)  (0) 2022.07.02