February 2018
Intermediate to advanced
350 pages
7h 35m
English
As discussed previously, classes can have a state. In Kotlin, a class's state is represented by properties. Let's have a look at the blueberry cupcake example:
class BlueberryCupcake { var flavour = "Blueberry"}
The BlueberryCupcake class has an has-a property flavour of type String.
Of course, we can have instances of the BlueberryCupcake class:
fun main(args: Array<String>) { val myCupcake = BlueberryCupcake() println("My cupcake has ${myCupcake.flavour}")}
Now, because we declare the flavour property as a variable, its internal value can be changed at runtime:
fun main(args: Array<String>) { val myCupcake = BlueberryCupcake() myCupcake.flavour = "Almond" println("My cupcake has ${myCupcake.flavour}")}
That is impossible in ...
Read now
Unlock full access