In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge points of this article "how to use the by keyword of Kotlin", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use the by keyword of Kotlin" article.
The delegate pattern has proved to be a good alternative to inheritance.
By in Kotlin is used to implement delegates.
Fun main (args: Array) {
Val b = BaseImpl ("base")
Derived (b) .print ()
}
Interface Base {
Fun print ()
}
Class BaseImpl (val x: String): Base {
Override fun print () = print (x)
}
Class Derived (b: Base): Base by b
The by clause in the parent class list of Derived stores b inside the Derived, and the compiler generates all methods of type b
Override interface members implemented by the delegate
Fun main (args: Array) {
Val b = BaseImpl ("base")
Val derived = Derived (b)
Derived.printLine () / / base----base
Derived.printMessage () / / Derived
Println (derived.message) / / Derived
}
Interface Base {
Val message: String
Fun printMessage ()
Fun printLine ()
}
Class BaseImpl (val x: String): Base {
Overrideval message: String= "base----$x"
Override fun printMessage () = print (x)
Override fun printLine () = println (message)
}
Class Derived (b: Base): Base by b {
Overrideval message: String= "Derived----"
Override fun printMessage () = print ("Derived")
}
Derived can override methods, but overridden members are not called from members of the delegate object
Attribute delegation
Syntax for attribute delegation: val/var: by
Property delegates do not need to implement any interfaces, but override the setValue and getValue methods
Class Delegate {
Operator fun getValue (thisRef: Any?, property: KProperty): String {
Return "${thisRef?.javaClass}, thank you for delegating'${property.name}'to me!"
}
Operator fun setValue (thisRef: Any?, property: KProperty, value: String) {
Println ("$value has been assigned to'${property.name}'in ${thisRef?.javaClass}.")
}
}
Fun main (args: Array) {
Val example=Example ()
Print (example.str) / / class sample.xietaichen.koinsample.Example, thank you for delegating 'str' to me!
Example.str= "aaa" / / aaa has been assigned to 'str' in sample.xietaichen.koinsample.Example@19469ea2.
}
Class Example {
Var str: String by Delegate ()
}
When we read from the str delegated to a Delegate instance, we will call the getValue () function in Delegate, so its first argument is to read out the object of the str, and the second parameter holds the description of the str itself (for example, you can take its name).
SetValue is the same.
For a read-only property (that is, declared by val), the delegate must provide a function named getValue, which accepts the following parameters:
ThisRef-must be the same as the property owner type (in the case of an extended property-the type being extended) or its supertype
Property-must be type KProperty or its supertype.
This function must return the same type (or its subtype) as the property
For a mutable attribute (that is, declared by var), the delegate must provide an additional function named setValue, which accepts the following parameters:
ThisRef-same as getValue ()
Property-same as getValue ()
New value-must be of the same type as the property or its supertype.
The getValue () or / and setValue () functions can be provided by delegating class member functions or by extension functions. The latter is more convenient when you need to delegate properties to objects of these functions that are not originally provided. Both functions need to be marked with the operator keyword.
The delegate class can implement one of the ReadOnlyProperty or ReadWriteProperty interfaces that contain the desired operator methods
Interface ReadOnlyProperty {
Operator fun getValue (thisRef: r, property: KProperty): t
}
Interface ReadWriteProperty {
Operator fun getValue (thisRef: r, property: KProperty): t
Operator fun setValue (thisRef: r, property: KProperty, value: t)
}
The above is about the content of this article "how to use the by keyword of Kotlin". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to 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.