Читать книгу Programming Kotlin Applications - Бретт Мак-Лахлин, Brett McLaughlin - Страница 69
OVERRIDING PROPERTY ACCESSORS AND MUTATORS
ОглавлениеWith what you know about types, and strong types in particular, you're finally ready to get back to that annoying little detail of Person
: if you set the last name outside the Person
constructor, printing the class instance gives the original last name, not the modified one. As a refresher, here's the code in question:
// Create another person val rose = Person("Rose", "Bushnell", 56.8, 32, true) println(rose) // Change Rose's last name rose.lastName = "Bushnell-Truesby" println(rose)
The result? This:
Rose Bushnell Rose Bushnell
What really needs to happen here? Well, every time that a last name—or first name, for that matter—is updated, the full name of the person that the instance represents needs to be updated. Seems simple enough, right?
Well, hold on, because this is going to get fairly complicated really fast. You're about to enter the somewhat unusual world of overriding mutators and accessors in Kotlin.