1. classes


1-1) Abstract

Abstract class (추상 클래스) : 다른 클래스가 상속받을 수 있는 클래스

하지만 이 클래스는 직접 새로운 인스턴스를 만들 수 없다.

eg.

Abstract class User {
  constructor(
   private firstName : string,
   private lastName : string,
   private nickname : string
   // private property들은 인스턴스 밖에서 접근할 수 없고, 다른 자식 클래스에서도 접근이 불가능하다
  ) {}
  abstract getNickName() : void
   // abstract method (추상 메소드)
   // 추상 메소드는 추상 클래스를 상속받는 모든 것들이 구현을 해야하는 메소드를 의미한다.
   // 즉, 구현되어있지 않은 메소드이다.
  getFullName(){
   return `${this.firstName} ${this.lastName}`
  }
}

const son = new User('heungmin','son','sonny'); 
// error(이 클래스는 직접 새로운 인스턴스를 만들 수 없다.)

class Player extends User {
  getNickName(){
   console.log(this.nickname); // error (property가 private이기 때문이다.)
  }
}

const son = new Player('heungmin','son','sonny');

private를 protected로 바꾸면 에러가 발생하지 않는다.

absstract class User {
  constructor(
   protected firstName : string,
   protected lastName : string,
   protected nickname : string
   // 필드가 외부로부터는 보호되지만, 
   // 다른 자식 클래스에서는 사용되기를 원한다면 private 대신 protected를 사용한다.
  ) {}
  getFullName(){
   return `${this.firstName} ${this.lastName}`
  }
}

class Player extends User {
  getNickName(){
   console.log(this.nickname); 
  }
}

const son = new Player('heungmin','son','sonny');

son.getFullName()
son.firstName(); // error (protected이기 때문에 접근 불가능)

1-2) implements

implements: 특정 인터페이스(또는 추상 타입)를 구현