July 2018
Intermediate to advanced
400 pages
12h 14m
English
So far, you have seen a property initialized in two ways – either assigned to a value passed as an argument, or defined inline in a primary constructor.
A property can (and must) be initialized with any value of its type, including function return values. Let’s look at an example.
Your hero can come from one of any number of exotic locales in the world of NyetHack. Define a new String property called hometown to hold the name of a player’s town of origin.
Listing 13.9 Defining the hometown property (Player.kt)
class Player(_name: String, var healthPoints: Int = 100 val isBlessed: Boolean private val isImmortal: Boolean) { var name = _name get() = field.capitalize() private set(value) { field = value.trim() ...Read now
Unlock full access