15 lines
425 B
TypeScript
Raw Normal View History

2020-05-24 17:04:37 +02:00
import { Dog } from "./dog";
import { Cat } from "./cat";
2020-05-24 18:44:11 +02:00
import { Animal } from "./animal";
2020-05-24 17:04:37 +02:00
2020-05-24 18:44:11 +02:00
// 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')
]
2020-05-24 17:04:37 +02:00
2020-05-24 18:44:11 +02:00
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();
}