In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of how to use the Kotlin expansion function, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value, I believe you will gain something after reading this article on how to use the Kotlin expansion function. Let's take a look at it.
Principle
Extension function is a common feature in kotlin. For example, we can extend a toast method to Context:
/ MainActivity.ktfun Context.toast (msg: String) {Toast.makeText (this, msg, Toast.LENGTH_SHORT). Show ()} private fun foo (context: Context) {context.toast ("hello world")}
Its principle is actually very simple, is to generate a toast method. The this pointer to the extension function is actually the first argument to this generation method:
/ * compiled from: MainActivity.kt * / public final class MainActivityKt {public static final void toast (Context $this$toast, String msg) {/ / Parameter null. / / extension function code Toast.makeText ($this$toast, msg, 0). Show ();}}
So the this pointer is actually an object reference passed in from the place where the function is called:
Private final void foo (Context context) {MainActivityKt.toast (context, "hello world");} restrictions
After knowing the implementation principle of the extension function, we can understand the limitations of the extension function from the principle.
Cannot access private members
Because after compiling to java, the generated extension method actually relies on the first parameter in and out of the object reference, and then uses this object reference to call the object's method. Therefore, we do not have permission to call private methods in the extension function:
Class TestClass {fun publicFun () {} private fun privateFun () {}} fun TestClass.extFun () {publicFun () / / correct, can call public method privateFun () / / error, cannot call private method} extension function cannot implement polymorphism
Because the extension function does not really add a member function to the class, the extension function of the same name of the parent class and subclass does not have the property of polymorphism.
For example, we extend the same foo () function for both parent and subclasses:
Open class Parentclass Child: Parent () fun Parent.foo () {println ("parent")} fun Child.foo () {println ("child")}
Then, as long as you convert the subclass to the parent class, the extension function called is the extension function of the parent class:
Val child = Child () child.foo () (child as Parent). Foo () / / output: / / the member function of child// parent has high priority, and the extension function cannot be rewritten.
When the extension function is the same as the class itself or the member function of the parent class, the member function will be called first in the actual call, and there will be no similar rewriting effect.
For example, we write an extension function for a class that is the same as the member function, and actually call the class member function first:
Open class Parent {fun foo () {println ("foo")}} fun Parent.foo () {println ("parent")} Parent () .foo () / output: / / foo
Even if you write an extension function for the subclass that is the same as the member function of the parent class, the member function of the parent class is called first:
Open class Parent {fun foo () {println ("foo")} class Child: Parent () fun Child.foo () {println ("child")} Child (). Foo () / output: / / foo turn off why you want to use the extension function in Kotlin
We all know that the Koltin language can be very interoperable with Java, so the new feature of extension functions can be smoothly integrated with existing Java code. Even pure Kotlin projects can be built on Java libraries, or even some framework libraries in Android, third-party libraries. Extension functions are well suited to the mixed development model of the Kotlin and Java languages. In many companies, some stable and good libraries are written in Java, and there is no need to rewrite them in Kotlin. But if you want to extend the interface and functionality of the library, the extension function may come in handy. Another advantage of using Kotlin extension functions is that there are no side effects and will not affect the original library code or functionality. Let's take a look at what the extension function looks like.
A simple example of setting bold for TextView
/ / extension function definition fun TextView.isBold () = this.apply {paint.isFakeBoldText = true} / / extension function call activity.find (R.id.course_comment_tv_score). This is the end of the article on "how to use Kotlin extension functions" by isBold (). Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use the Kotlin expansion function". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.