官术网_书友最值得收藏!

Inheritance in TypeScript

We just saw how to implement an inheritance in JavaScript using a prototype. Now, we will see how an inheritance can be implemented in TypeScript, which is basically ES6 inheritance.

In TypeScript, similar to extending interfaces, we can also extend a class by inheriting another class, as follows:

class SimpleCalculator { 
   z: number; 
    constructor() { } 
   addition(x: number, y: number) { 
        this.z = this.x + this.y; 
   } 
    subtraction(x: number, y: number) { 
        this.z = this.x - this.y; 
   } 
} 
  
class ComplexCalculator extends SimpleCalculator { 
    constructor() { super(); } 
   multiplication(x: number, y: number) { 
        this.z = x * y; 
   } 
    division(x: number, y: number) { 
        this.z = x / y; 
   } 
} 
var calculator = new ComplexCalculator(); 
calculator.addition(10, 20); 
calculator.Substraction(20, 10); 
calculator.multiplication(10, 20); 
calculator.division(20, 10); 

Here, we are able to access the methods of SimpleCalculator using the instance of ComplexCalculator as it extends SimpleCalculator.

主站蜘蛛池模板: 九江市| 江安县| 兴业县| 舟曲县| 海口市| 琼结县| 昆明市| 临清市| 双鸭山市| 西峡县| 杨浦区| 临猗县| 台东市| 巧家县| 穆棱市| 玛多县| 左云县| 宁河县| 沙雅县| 东兴市| 高阳县| 河南省| 鄂温| 定远县| 同心县| 张家港市| 丰县| 西林县| 金秀| 上栗县| 田林县| 稷山县| 睢宁县| 泸溪县| 卓尼县| 且末县| 凤山市| 甘肃省| 大理市| 志丹县| 百色市|