Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. class AnimalManager {
  2. instances := []
  3.  
  4. __New() {
  5.  
  6. }
  7.  
  8. init() {
  9. this.instances.insert(new Dog())
  10. this.instances.insert(new Cat())
  11. }
  12.  
  13. feedAnimals() {
  14. for k, _animal in this.instances {
  15. // if you can, type check for safety in case you want to do many things
  16. // if (_animal.__Class == "Animal") {
  17. // if (_animal.feed) {
  18. _animal.feed()
  19. // }
  20. }
  21. }
  22. }
  23.  
  24. class Animal {
  25. feed(howMuch) {
  26. throw Exception("I don't know how to eat yet");
  27. }
  28. }
  29.  
  30. class Dog extends Animal {
  31. feed(howMuch) {
  32. msgbox % "I'm eating " . howMuch . " food."
  33. }
  34. }
  35.  
  36. class Cat extends Animal {
  37. feed(howMuch) {
  38. msgbox % "I'm eating " . (howMuch/2) . " food because I'm a picky eater."
  39. }
  40. }
  41.  
  42. mgr := new AnimalManager ()
  43.  
  44. mgr.init();
  45. mgr.feedAnimals();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement