TypeScript) 클래스

송민경's avatar
Sep 13, 2024
TypeScript) 클래스

1. 클래스 만들기

  • private : 클래스 외부에서 접근할 수 없음
class Player { constructor( private firstName:string, private lasttName:string ) { } }
notion image
  • public : 기본 접근 제어자이며, 클래스 외부에서도 접근할 수 있음
class Player { constructor( private firstName:string, private lasttName:string, public nickName:string ) { } } const min = new Player("min", "las", "밍"); min.firstName;
notion image
 

2. 추상 클래스

  • 인스턴스를 생성할 수 없으며, 다른 클래스에서 상속받아 사용해야 함
abstract class User{ constructor( private firstName:string, private lasttName:string, public nickName:string ) {} } class Player extends User { } const min = new User("min", "las", "밍");
notion image
 

3. 추상 메서드

  • 추상 클래스 내에 선언
  • 이 메서드는 상속받는 클래스에서 반드시 구현해야 함
    • abstract class User{ constructor( private firstName:string, private lasttName:string, public nickName:string ) {} getFullName() { return`${this.firstName} ${this.lasttName}` } } class Player extends User { } const min = new Player("min", "las", "밍"); min.getFullName()
      notion image
  • 구현하지 않으면 클래스 자체도 추상 클래스가 되어 인스턴스를 생성할 수 없음
abstract class User{ constructor( private firstName:string, private lasttName:string, public nickName:string ) {} abstract getNickName():void // 콜 시그니처 getFullName() { return`${this.firstName} ${this.lasttName}` } } class Player extends User { } const min = new Player("min", "las", "밍"); min.getFullName()
notion image
  • private이기에 직접 접근할 수 없음
abstract class User{ constructor( private firstName:string, private lasttName:string, private nickName:string ) {} abstract getNickName():void // 콜 시그니처 getFullName() { return`${this.firstName} ${this.lasttName}` } } class Player extends User { getNickName() { console.log(this.nickName); } } const min = new Player("min", "las", "밍"); min.getFullName()
notion image
Share article
RSSPowered by inblog