面试题答案
一键面试// 定义接口
interface Shape {
area: number;
calculateArea(): void;
}
// 定义基类
class TwoDShape implements Shape {
area: number;
width: number;
height: number;
constructor(width: number, height: number) {
this.width = width;
this.height = height;
this.area = 0;
}
calculateArea(): void {
this.area = this.width * this.height;
}
}
// 定义子类
class Triangle extends TwoDShape {
calculateArea(): void {
this.area = (this.width * this.height) / 2;
}
}