추상클래스
자식클래스가 강하게 자라길 바라는 부모 클래스
추상클래스는 클래스와 다르게 인스턴스화를 할 수 없다.
인스턴스 할 수 없는 클래스를 왜 써야할 까요?
기본 메서드는 정의하지만 핵심은 자식이 하라고 위임하여 강하게 키운다.
ABSTRACT키워드를 사용하고 정의한다.
abstract class Shape {
abstract getArea(): number; // 추상 함수 정의!!!
printArea() {
console.log(`도형 넓이: ${this.getArea()}`);
}
}
class Circle extends Shape {
radius: number;
constructor(radius: number) {
super();
this.radius = radius;
}
getArea(): number { // 원의 넓이를 구하는 공식은 파이 X 반지름 X 반지름
return Math.PI * this.radius * this.radius;
}
}
//넓이구하는 공식을 정의
const circle = new Circle(5);
circle.printArea();
class Rectangle extends Shape {
width: number;
height: number;
constructor(width: number, height: number) {
super();
this.width = width;
this.height = height;
}
getArea(): number { // 사각형의 넓이를 구하는 공식은 가로 X 세로
return this.width * this.height;
}
}
const rectangle = new Rectangle(4, 6);
rectangle.printArea();
어렵다 어려워 더 봐야할 듯.