Extension function cho phép bạn thêm method mới vào class có sẵn mà không cần kế thừa hay sửa code gốc.
Ví dụ có thể thêm method thẳng vào class String: fun String.isPhoneNumber(): Boolean { return this.length == 10 }.
Extension functions allow you to add new methods to existing classes without inheritance or modifying the original class.
- For example:
fun String.isPhoneNumber(): Boolean { return this.length == 10 }. - The function name is prefixed with the class you're extending, and
thisrefers to the calling instance. - They're extremely useful in Android for creating helper methods on platform classes.