polymorphism completed
This commit is contained in:
5
typescript/Polymorphism.md
Normal file
5
typescript/Polymorphism.md
Normal file
@ -0,0 +1,5 @@
|
||||
# Polymorphism
|
||||
|
||||
Relevant files
|
||||
|
||||
- [index.ts](./inheritance/index.ts)
|
@ -2,7 +2,6 @@
|
||||
|
||||
Relevant files:
|
||||
|
||||
- [index.ts](./index.ts)
|
||||
- [animal.ts](./animal.ts)
|
||||
- [cat.ts](./cat.ts)
|
||||
- [dog.ts](./dog.ts)
|
||||
|
@ -1,8 +1,14 @@
|
||||
import { Dog } from "./dog";
|
||||
import { Cat } from "./cat";
|
||||
import { Animal } from "./animal";
|
||||
|
||||
const d = new Dog('buster');
|
||||
const c = new Cat('mittens');
|
||||
// because cat and dog are subclasses of animal, they can be stored in an array of animals
|
||||
const animals: Animal[] = [
|
||||
new Dog('buster'),
|
||||
new Cat('mittens')
|
||||
]
|
||||
|
||||
d.make_sound();
|
||||
c.make_sound();
|
||||
for (const a of animals) {
|
||||
// when calling the make_sound function, the implementation of the actual class gets called (in this case cat or dog)
|
||||
a.make_sound();
|
||||
}
|
||||
|
Reference in New Issue
Block a user