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

  • Expert Angular
  • Mathieu Nayrolles Rajesh Gunasundaram Sridhar Rao
  • 145字
  • 2021-07-15 17:05:32

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.

主站蜘蛛池模板: 瑞丽市| 聊城市| 交城县| 高青县| 贵溪市| 哈尔滨市| 苏尼特左旗| 盘山县| 巴楚县| 昌吉市| 保康县| 收藏| 老河口市| 瑞安市| 伊宁市| 平湖市| 浦北县| 杭锦后旗| 苏尼特右旗| 崇仁县| 苍梧县| 奇台县| 荃湾区| 吴江市| 蕲春县| 塘沽区| 木兰县| 寿光市| 德州市| 汉寿县| 新巴尔虎左旗| 松滋市| 阳谷县| 酒泉市| 延长县| 临西县| 阿勒泰市| 扎兰屯市| 盐源县| 溆浦县| 呼图壁县|