October 2019
Intermediate to advanced
434 pages
11h 54m
English
Like in Java, and other OOP languages, Kotlin classes can contain member functions (methods). To add a method to a class, we can simply define a function within the class body. Here, we've added a method named printStudentInfo to the Student class:
class Student(_firstName: String, val lastName: String) { ... fun printStudentInfo() { println("id:$id -> $firstName $lastName") }}
We can then invoke the printStudentInfo() method on an instance of the Student class by referencing the instance of Student, followed by a . and then the method name and any required arguments, as in the following example:
fun main(args: Array<String>) { val student = Student("Nate", "Ebel") student.printStudentInfo()}
To change the visibility of a method, ...
Read now
Unlock full access