Kotlin for Android Developers
Download 1.04 Mb. Pdf ko'rish
|
Kotlin for Android Developers Learn Kotlin the Easy Way While Developing an Android App ( PDFDrive )
7.3 Extension functions
An extension function is a function that adds a new behaviour to a class, even if we don’t have access to the source code of that class. It’s a way to extend classes which lack some useful functions. In Java, this is usually implemented in utility classes which include a set of static methods. The advantage of using extension functions in Kotlin is that we don’t need to pass the object as an argument. The extension function acts as if it belonged to the class, and we can implement it using this and all its public methods. For instance, we can create a toast function which doesn’t ask for the context, which could be used by any Context objects, and those whose type extends Context , such as Activity or Service : 1 fun Context.toast(message: CharSequence, duration: Int = Toast.LENGTH_SHORT) { 2 Toast.makeText(this, message, duration).show() 3 } This function can be used inside an activity, for instance: 1 toast("Hello world!") 2 toast("Hello world!", Toast.LENGTH_LONG) Of course, Anko already includes its own toast extension function, very similar to this one. Anko provides functions for both CharSequence and resources, and different functions for short and long toasts: 1 toast("Hello world!") 2 longToast(R.id.hello_world) Extension functions can also be properties. So you can create extension properties too in a very similar way. The following example is showing a way to generate a property with its own getters and setters. Kotlin already provides this property for us as an interoperability feature, but it’s a good exercise to understand the idea behind extension properties: 1 public var TextView.text: CharSequence 2 get() = getText() 3 set(v) = setText(v) Extension functions don’t really modify the original class, but the function is added as a static import where it is used. Extension functions can be declared in any file, so a common practice is to create files which include a set of related functions. And this is the magic behind many Anko features. From now own, you can create your own magic too. |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling