当前位置:网站首页>Kotlin delegate property knowledge points
Kotlin delegate property knowledge points
2022-08-08 20:33:00 【Do not forget the original intention z】
Kotlin delegate property keyword By lazy
By is the keyword of the delegate property, the code is as follows:
class user {
var name :String by Delegate()
}
This is to delegate name to Delegate(), now create a Delegate class
class Delegate{
Two methods will be created in it
setValue () {
}
and getValue(){
} method The compiler will let you automatically generate
}
How to use it
fun main() { val user = User()
user.name = "Zhang San"
println(user.name)
}
The print result is as follows: Zhang San
by lazy lazy loading attribute
The type of lazy-loaded properties is similar to that of lazy-initialized properties. The initialization operation can only be performed when the property is accessed for the first time. The lazy-loaded property application uses the lazy function to apply for the delegated property, and the delayed-initialized property uses the lateinit keyword.There is a certain difference between the two
lazy loading properties must be val properties
lateinit properties must be var properties
use the code as follows
val user : String by lazy {
println(this method is executed only once)
Hello
}
fun main(){
println (user)
println (user)
}
The print result is as follows
This method is executed only once
The above assignment is executed only once
The above assignment is executed only once
Seeing the above results, the conclusion is that the first line of code above is only executed once. Note that the method followed by the curly braces can be a lambda expression
Now
Observable Properties
var name:String by XXX.observable("None") { porp,oldValue,newValue ->
println("$oldValue 0> $newValue")
}
fun main(){
name = "Zhang San"
name = "Zhang Si"
}
The print result is as follows:
None -> Zhang San
Zhang San -> Zhang Si
Conclusion: It can be clearly seen from the print result, in fact, this method can observe the attribute to change process, just like from android to JetPack to ViewModel, it can be very simple to implement UI interface to update
Another method is that vetoable can intercept an assignment process and reject it.
Store attributes in a map
It is often used to store the value of the attribute in the mapping map.This is often used in json or other dynamic applications, in which case the map instance itself can be used as a delegate to implement the delegate property
class User(val map: Map
val name : String by map
val age : Int by map If age is cancelled, an error will occur
}
Usage:
val user =User(mapOf(“name” to “Zhang San”, “age” to 20))
边栏推荐
猜你喜欢
随机推荐
新库上线 | CnOpenData信息传输、软件和信息技术服务业工商注册企业基本信息数据
小白如何购买基金产品?
What are the role of document management system for companies?
学习笔记:XML
源码分析MyCat专栏
PHP传递任意数量的函数参数
Bluu海鲜公司推出首批实验室培育的鱼类产品
【无标题】
买股票安全吗 资金能取出来吗
leveldb-impl:level0
随手记:laravel、updateOrCreate 和 updateOrInsert 的区别
Categorized input and output, Go lang1.18 introductory refining tutorial, from Bai Ding to Hongru, go lang basic data types and input and output EP03
com.alibaba.fastjson.JSONException: default constructor not found. class
莫让“学院派”限制我们的思维:在数组的中间位置删除数据一定比链表慢?
正则表达式与文本处理器
技术分享活动
Mysql管理指令
什么是仿射函数?
如何用精益敏捷组合管理,提升研发效能?软件研发团队必看!
Maykel Studio OpenHarmony Device Development Training Notes - Chapter 6 Study Notes