Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class AnimalManager {
- instances := []
- __New() {
- }
- init() {
- this.instances.insert(new Dog())
- this.instances.insert(new Cat())
- }
- feedAnimals() {
- for k, _animal in this.instances {
- // if you can, type check for safety in case you want to do many things
- // if (_animal.__Class == "Animal") {
- // if (_animal.feed) {
- _animal.feed()
- // }
- }
- }
- }
- class Animal {
- feed(howMuch) {
- throw Exception("I don't know how to eat yet");
- }
- }
- class Dog extends Animal {
- feed(howMuch) {
- msgbox % "I'm eating " . howMuch . " food."
- }
- }
- class Cat extends Animal {
- feed(howMuch) {
- msgbox % "I'm eating " . (howMuch/2) . " food because I'm a picky eater."
- }
- }
- mgr := new AnimalManager ()
- mgr.init();
- mgr.feedAnimals();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement