面试题答案
一键面试抽象类
- 适用场景:当多个图形类有一些共同的属性或方法实现,并且这些图形类本质上属于同一类事物,需要有一个共同的基类来进行统一管理时,使用抽象类。例如,所有图形都可能有颜色这个属性,并且可能有一个共同的绘制到画布的基础实现逻辑。
- 代码示例:
// 抽象图形类
abstract class Shape {
color: string;
constructor(color: string) {
this.color = color;
}
// 抽象方法,子类必须实现
abstract calculateArea(): number;
// 具体实现的方法
drawOnCanvas(): void {
console.log(`Drawing ${this.constructor.name} with color ${this.color}`);
}
}
// 圆形类继承自抽象图形类
class Circle extends Shape {
radius: number;
constructor(color: string, radius: number) {
super(color);
this.radius = radius;
}
calculateArea(): number {
return Math.PI * this.radius * this.radius;
}
}
// 矩形类继承自抽象图形类
class Rectangle extends Shape {
width: number;
height: number;
constructor(color: string, width: number, height: number) {
super(color);
this.width = width;
this.height = height;
}
calculateArea(): number {
return this.width * this.height;
}
}
// 使用示例
const circle = new Circle('red', 5);
circle.drawOnCanvas();
console.log(`Circle area: ${circle.calculateArea()}`);
const rectangle = new Rectangle('blue', 4, 6);
rectangle.drawOnCanvas();
console.log(`Rectangle area: ${rectangle.calculateArea()}`);
接口
- 适用场景:当只关注对象具有某些特定的行为(方法),而不关心这些对象的共同基类或属性时,使用接口。比如,不同类型的图形,它们的绘制逻辑和面积计算逻辑完全不同,只是都需要实现计算面积和绘制这两个行为。
- 代码示例:
// 图形接口
interface ShapeInterface {
calculateArea(): number;
drawOnCanvas(): void;
}
// 圆形类实现图形接口
class Circle implements ShapeInterface {
radius: number;
constructor(radius: number) {
this.radius = radius;
}
calculateArea(): number {
return Math.PI * this.radius * this.radius;
}
drawOnCanvas(): void {
console.log(`Drawing circle with radius ${this.radius}`);
}
}
// 矩形类实现图形接口
class Rectangle implements ShapeInterface {
width: number;
height: number;
constructor(width: number, height: number) {
this.width = width;
this.height = height;
}
calculateArea(): number {
return this.width * this.height;
}
drawOnCanvas(): void {
console.log(`Drawing rectangle with width ${this.width} and height ${this.height}`);
}
}
// 使用示例
const circle = new Circle(5);
circle.drawOnCanvas();
console.log(`Circle area: ${circle.calculateArea()}`);
const rectangle = new Rectangle(4, 6);
rectangle.drawOnCanvas();
console.log(`Rectangle area: ${rectangle.calculateArea()}`);
选择原因总结:抽象类适合有共同属性和部分共同实现逻辑的场景,接口适合只关注特定行为实现,不关心类继承体系和共同属性的场景。在大型项目中,如果图形类有较多共同部分,抽象类能更好地复用代码和进行统一管理;如果图形类差异较大,只是需要有统一的行为规范,接口更合适。