Skip to content

Instantly share code, notes, and snippets.

@joanmolinas
Created March 8, 2018 19:41
Show Gist options
  • Save joanmolinas/5037dc3f22ed1190b8b2b20009df4f55 to your computer and use it in GitHub Desktop.
Save joanmolinas/5037dc3f22ed1190b8b2b20009df4f55 to your computer and use it in GitHub Desktop.
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