Class method redefinition In Swift
Forums:
class Shape {
func getArуa() -> Int {
return 0
}
}
class Square: Shape {
var sideLength: Int
init (sideLength: Int) {
self.sideLength = sideLength
}
override func getArуa() -> Int {
if sideLength > 0 {
return sideLength * sideLength
}
//call base class method
return super.getArуa()
}
}
let square = Square(sideLength: 5)
let area = square.getArуa()
//area is 25
- Log in to post comments