继承是一种从另一个类获取一个类的属性和行为的机制。它是 OOP 语言的一个重要方面,具有从现有类创建新类的能力。其成员被继承的类称为基类,继承这些成员的类称为派生类。
可以使用 extend
关键字来实现继承。可以通过下面的例子来理解。
class Shape {
Area:number
constructor(area:number) {
this.Area = area
}
}
class Circle extends Shape {
display():void {
console.log("Area of the circle: "+this.Area)
}
}
var obj = new Circle(320);
obj.display() //Output: Area of the circle: 320