Advertisement
Guest User

Untitled

a guest
Mar 27th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Nim 0.59 KB | None | 0 0
  1. type Animal = ref object of RootObj
  2.   name: string
  3.   age: int
  4. method vocalize(this: Animal): string {.base.} = "..."
  5. method ageHumanYrs(this: Animal): int {.base.} = this.age
  6.  
  7. type Dog = ref object of Animal
  8. method vocalize(this: Dog): string = "woof"
  9. method ageHumanYrs(this: Dog): int = this.age * 7
  10.  
  11. type Cat = ref object of Animal
  12. method vocalize(this: Cat): string = "meow"
  13.  
  14.  
  15. var animals: seq[Animal] = @[]
  16. animals.add(Dog(name: "Sparky", age: 10))
  17. animals.add(Cat(name: "Mitten", age: 10))
  18.  
  19. for a in animals:
  20.   case a:
  21.     of Dog:
  22.       echo "dog"
  23.     of Cat:
  24.       echo "cat"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement