Created
October 27, 2014 03:46
-
-
Save rtoal/c254a6e2d8c02ae68864 to your computer and use it in GitHub Desktop.
An example of inheritance and polymorphism in CoffeeScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A typical example of inheritance | |
class Animal | |
constructor: (@name) -> | |
speak: -> | |
"#{@name} says #{this.sound()}" | |
class Cow extends Animal | |
sound: -> "moo" | |
class Horse extends Animal | |
sound: -> "neigh" | |
class Sheep extends Animal | |
sound: -> "baaaaa" | |
s = new Horse "CJ" | |
console.log s.speak() | |
c = new Cow "Bessie" | |
console.log c.speak() | |
console.log new Sheep("Little Lamb").speak() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment