I have try to override variable in subclass many times, but never try override getter/setter before today, this is my Swift code in Playground :
class Person {
var name: String {
didSet {
print("Superclass did Set")
}
willSet {
print("Superclass will Set")
}
}
init(name: String) {
self.name = name
}
}
class Student: Person {
override var name: String {
didSet {
print("Subclass did Set")
}
willSet {
print("Subclass will Set")
}
}
}
let apple = Student(name: "John")
apple.name = "Apple"
The results like below:
Subclass will Set
Superclass will Set
Superclass did Set
Subclass did Set
That's mean we can't override variable setter/getter?