Created
February 14, 2022 09:59
-
-
Save hellokvn/2f2306075b76d729128a20d417f07b7b to your computer and use it in GitHub Desktop.
Polymorphism - Good
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Animal { | |
public name: string; | |
constructor(name: string) { | |
this.name = name; | |
} | |
public makeSound(): void { | |
process.stdout.write('generic animal sound\n'); | |
} | |
} | |
export class Dog extends Animal { | |
public makeSound(): void { | |
process.stdout.write('wuff wuff\n'); | |
} | |
} | |
class Cat extends Animal { | |
public makeSound(): void { | |
process.stdout.write('meow meow\n'); | |
} | |
} | |
const pocky: Cat = new Cat('Pocky'); | |
pocky.makeSound(); // -> meow meow | |
const toshii: Dog = new Dog('Pocky'); | |
toshii.makeSound(); // -> wuff wuff |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment