Skip to content

Instantly share code, notes, and snippets.

@joanmolinas
Created March 8, 2018 19:38
Show Gist options
  • Save joanmolinas/29a215a7b3e634663213c2002d9579ec to your computer and use it in GitHub Desktop.
Save joanmolinas/29a215a7b3e634663213c2002d9579ec to your computer and use it in GitHub Desktop.
class RectangleField {
var width: Float = 0
var height: Float = 0
var area: Float {
return width * height
}
}
class SquareField: RectangleField {
override var area: Float {
return width * width
}
}
func printField(field: Field) {
print(field.area)
}
let field = RectangleField()
field.width = 2
field.height = 4
printField(field: field) // 8
let squareField = SquareField()
squareField.width = 2
squareField.height = 4
printField(field: squareField) // 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment