January 2018
Intermediate to advanced
434 pages
14h 1m
English
One thing to note here is that you cannot implement a custom getter or setter for your property in the constructor. You need to declare a property in the body of the class:
class Student(val name: String, age: Int) { var age: Int = age set(value) { println("Setting age to $value") field = value }}
One key thing to note here is that you need to keep the visibility of the getter exactly similar to the visibility of the property:
protected var name="aanand"protected get() = field.toUpperCase()
The preceding code is perfectly valid, though it's redundant to place the same access modifier again, hence it's better to omit it.
The setter, on the other hand, can have an access modifier less permissive than the property. Consider ...
Read now
Unlock full access