Created
March 8, 2018 19:41
-
-
Save joanmolinas/5037dc3f22ed1190b8b2b20009df4f55 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
protocol Field { | |
var area: Float { get } | |
} | |
class RectangleField: Field { | |
var width: Float = 0 | |
var height: Float = 0 | |
var area: Float { | |
return width * height | |
} | |
} | |
class SquareField: Field { | |
var side: Float = 0 | |
var area: Float { | |
return side * side | |
} | |
} | |
func printField(field: Field) { | |
print(field.area) | |
} | |
let field = RectangleField() | |
field.width = 2 | |
field.height = 4 | |
printField(areable: field) // 8 | |
let squareField = SquareField() | |
squareField.side = 2 | |
printField(areable: squareField) // 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment