Last active
February 20, 2017 05:18
-
-
Save vvviiimmm/ec8ff86d82a7d22a4eececbe27007333 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 inteface | |
| trait Shape { | |
| def area: Double | |
| } | |
| // Implementation 1 | |
| class Circle(radius: Double) extends Shape { | |
| override def area: Double = math.Pi * math.pow(radius, 2) | |
| } | |
| // Implementation 2 | |
| class Rectangle(width: Double, length: Double) extends Shape { | |
| override def area: Double = width * length | |
| } | |
| // Generic function | |
| def areaOf(shape: Shape): Double = shape.area | |
| // Usage | |
| areaOf(new Circle(10)) | |
| areaOf(new Rectangle(5, 5)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment