Inheritance of classes: object-oriented patterns
Super: Keywords
Class Animal {
name:string;
Constructor (thename:string) {
this.name = thename;
}
Move (Distanceinmeters:number = 0) {
console.log (' ${this.name} moved ${distanceinmeters}m. ');
}
Class Snack extends Animal {
Constructor (name:string) {
super (name);//Call constructor of parent class
}
// Overrides the parent class's method move
(Distanceinmeters = 5) {
console.log ("slithering ...");
Super.move (distanceinmeters);
}
Class Horse extends Animal {
Constructor (name:string) {
super (name);
}
Move (Distanceinmeters =) {
Console.log ("galloping");
Super.move (distanceinmeters);
}
Let Sam = new Snack ("Sammy the Python");
Let Tom:animal = new Horse ("Tommy the Palomino");
Sam.move ();
Tom.move ();
Modifiers: (in Typescript, members are default to public)
Public private: can only be accessed in the life of his class protect: readonly: Read-only properties must be initialized in a declaration or in a constructor in a derived class