Last active
June 3, 2020 07:49
-
-
Save vvviiimmm/bed7beadd194f0921113987a37e6789e 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
| trait Shape { | |
| def area: Double | |
| } | |
| // Shape definition data structures, should be in a diffrent file/namespace | |
| case class Circle(radius: Double) | |
| case class Rectangle(width: Double, length: Double) | |
| // Implementation 1 | |
| class CircleShape(radius: Double) extends Shape { | |
| override def area = math.Pi * math.pow(radius, 2) | |
| } | |
| // Implementation 2 | |
| class RectangleShape(width: Double, length: Double) extends Shape { | |
| override def area: Double = width * length | |
| } | |
| def areaOf(shape: Shape): Double = shape.area | |
| areaOf(new CircleShape(10)) | |
| areaOf(new RectangleShape(5, 5)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Should the class CircleShape(radius: Double) and RectangleShape(width: Double, length: Double) be CircleShape(circle: Circle) and RectangleShape(rectangle: Rectangle)?