Inheritance is a-to
Indicate a class receives behavior from a parent class. Then we can override, modify or augment
Those behaviors on the new class.
1 Class Report {2Data:array<string>;3 4Constructor (data:array<string>) {5 This. data =data;6 }7 8 run () {9 This. Data.foreach (function(line) {Console.log (line);});Ten } One}
Call the class:
var New Report ([' first line ', ' Second Line ']);
R.run ();
Result:
First Line Second Line
Now let's say we want to has a second report that takes some headers and some data but we still
Want to reuse how the report class presents the data to the user.
To reuse this behavior from the report class we can use inheritance with the Extends keyword:
Class Tabbedreport extends report { Headers:array<string>; Constructor (headers:string[], values:string[]) { this. Headers= headers; Super (values) //In report Class:data } Run () { console.log (headers); Super.run (); } }
Run:
var headers:string[] = [' Name ']; var data:string[] = [' Alice Green ', ' Paul pfifer ', ' Louis blakenship ']; var New Tabbedreport (headers, data) R.run ();
[TypeScript] Inheritance