Created
February 18, 2017 19:31
-
-
Save vvviiimmm/d0ad79af8d1a5a96a08aa0b5c1f46830 to your computer and use it in GitHub Desktop.
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
| // Our generic interface | |
| trait Animal { | |
| def voice: String | |
| } | |
| // Pure definitions | |
| case class Dog() | |
| case class Cat() | |
| // Implementation 1 | |
| class DogAnimal extends Animal { | |
| override def voice: String = "woof!" | |
| } | |
| // Implementation 2 | |
| class CatAnimal extends Animal { | |
| override def voice: String = "meow!" | |
| } | |
| // Generic function | |
| def say(animal: Animal) = println(animal.voice) | |
| // Usage | |
| say(new DogAnimal) // prints "woof!" | |
| say(new CatAnimal) // prints "meow!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment