- 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.
推薦閱讀
- iOS 9 Game Development Essentials
- Python自動化運維快速入門(第2版)
- Mastering Ember.js
- jQuery EasyUI網站開發實戰
- Learning SAP Analytics Cloud
- 數據結構簡明教程(第2版)微課版
- Java程序設計與計算思維
- Java EE 8 Application Development
- Visual FoxPro程序設計習題集及實驗指導(第四版)
- Android玩家必備
- Visual Basic 6.0程序設計實驗教程
- 自學Python:編程基礎、科學計算及數據分析(第2版)
- 深入淺出Python數據分析
- 軟件測試分析與實踐
- Java自然語言處理(原書第2版)