Advertisement
Guest User

Class Inheritance

a guest
Nov 28th, 2012
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Animal
  2.   constructor: (@name) ->
  3.  
  4.   move: (meters) ->
  5.     @position += meters
  6.     console.log @name + " moved #{meters}m."
  7.  
  8.   position: 0
  9.  
  10. class Snake extends Animal
  11.   slither: ->
  12.     @move(3)
  13.  
  14. class Horse extends Animal
  15.   gallop: ->
  16.     @move(10)
  17.  
  18. sam = new Snake "Sammy the Python"
  19. tom = new Horse "Tommy the Palomino"
  20.  
  21. sam.move(1)
  22. sam.slither()
  23. tom.gallop()
  24.  
  25. console.log("#{sam.name} moved #{sam.position}m total");
  26. console.log("#{tom.name} moved #{tom.position}m total");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement