Skip to content

Instantly share code, notes, and snippets.

@matiasdim
Created December 4, 2020 15:18
Show Gist options
  • Save matiasdim/512c428317ec20b8da6d228f84c9efe1 to your computer and use it in GitHub Desktop.
Save matiasdim/512c428317ec20b8da6d228f84c9efe1 to your computer and use it in GitHub Desktop.
class Circle {
var radius: Int
init(radius: Int) {
self.radius = radius
}
}
class Rectangle {
var width: Int
var height: Int
init(width: Int, height: Int) {
self.width = width
self.height = height
}
}
class AreaCalculator {
func area(shape: AnyObject) -> Double? {
if let rectangle = shape as? Rectangle {
return Double(rectangle.height * rectangle.width)
} else if let circle = shape as? Circle {
return Double.pi * Double(circle.radius * circle.radius)
}
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment