書名: 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.
推薦閱讀
- Docker and Kubernetes for Java Developers
- Power Up Your PowToon Studio Project
- Mastering Concurrency in Go
- The DevOps 2.4 Toolkit
- Oracle從入門到精通(第5版)
- Building Android UIs with Custom Views
- Microsoft Azure Storage Essentials
- C語言程序設計實訓教程與水平考試指導
- Mastering Elixir
- C++ System Programming Cookbook
- 黑莓(BlackBerry)開發從入門到精通
- 軟件測試技術
- 3ds Max 2018從入門到精通
- C語言程序設計
- PhantomJS Cookbook