October 2019
Intermediate to advanced
434 pages
11h 54m
English
We can add functions to a class to add functionality and to interact with that class. Like in Java, we can refer to a function on a class as a method. Methods are tied to the instance of the class they are associated with.
Defining a method is fundamentally the same as defining a top-level function, with the primary exception being that a method is declared within a class body. In the following example, we've defined a Person class that has a single method named printName():
class Person( val firstName: String, val lastName: String) { fun printName() { println("$firstName $lastName") }}
Note that the definition of printName doesn't look any different than the other top-level functions we've defined in this chapter.
To invoke ...
Read now
Unlock full access