TypeScript에서 OOP의 추상 메서드

하위 클래스가 공통 메서드를 가져야 하지만, 해당 메서드의 구현이 구체적인 하위 클래스에 따라 달라지는 경우가 있습니다. 이 경우 부모 추상 클래스에서 구현을 작성하지 않고 이 메서드를 선언할 수 있습니다. 그러면 하위 클래스는 이러한 메서드를 구현해야 합니다. 이러한 메서드를 추상 메서드라고 하며 abstract 키워드를 사용하여 선언됩니다.

예를 들어, 우리의 추상 클래스 User를 가져가 보겠습니다. 이 클래스의 상속자는 객체 데이터를 출력하는 show 메서드를 반드시 가져야 한다고 가정합시다.

그러나 이 메서드의 구현은 하위 클래스에 따라 달라집니다. User 클래스에서 이 메서드를 추상으로 선언해 보겠습니다:

typescriptabstract class User {
  public name: string;
  
  constructor(name: string) {
    this.name = name;
  }
  
  public abstract show(): string;
}

Student 하위 클래스에서 이 메서드를 구현해 보겠습니다:

typescriptclass Student extends User {
  public course: number;

  constructor(name: string, course: number) {
    super(name);
    this.course = course;
  }
  
  show() {
    return this.name + ' ' + this.course;
  }
}

Employee 하위 클래스에서 이 메서드를 구현해 보겠습니다:

typescriptclass Employee extends User {
  public salary: number;

  constructor(name: string, salary: number) {
    super(name);
    this.salary = salary;
  }
  
  show() {
    return this.name + ' ' + this.salary;
  }
}
Задание 1

추상 클래스 Figure에서 면적과 둘레를 얻기 위한 추상 메서드를 만드세요.

Задание 2

하위 클래스 Square 및 Rectangle에서 이러한 메서드의 구현을 작성하세요.