当前位置:网站首页>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))
边栏推荐
- 新库上线 | CnOpenDataA股上市公司基本信息数据
- 【翻译】宣布Kubernetes策略管理论文
- NAACL2022 NER SOTA - RICON study notes
- Kotlin反射
- 小白如何购买基金产品?
- 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
- 梅科尔工作室OpenHarmony设备开发培训笔记-第六章学习笔记
- Kotlin实用的一些框架
- LeetCode_67_二进制求和
- XTU OJ 1075 求最小公倍数
猜你喜欢
随机推荐
CVPR 2022 ActivityNet竞赛冠军:中科院深圳先进院提出高低分双模态行为识别框架
实践篇2:深度学习之----LetNet之tensorflow2的实现
fillder4不间断提示the system proxy was change,看我解决
JMeter测试接口并发场景
Questions about Mac terminal custom commands and Mysql command
Kotlin反射
编写CMakeLists生成静态库及可执行文件的make文件
解决执行Command报错fork/exec /xxx/yy: no such file or directory
学习笔记:线性表的顺序表示和实现(二级指针实现)
箭头函数this指向的解释
正则表达式与文本处理器
制作实例分割数据集
WPF主窗体调用 User32的SetWindowPos 设置窗体置顶会导致与其他窗体抢夺焦点的问题
XTU OJ 1075 求最小公倍数
测试面试题锦集
CSP-J2021 题解
正则表达式的限定符、或运算符、字符类、元字符、贪婪/懒惰匹配
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception
PHP传递任意数量的函数参数
西湖大学鞠峰组招聘【塑料降解 / 污水工程 / 微生物学】方向博士后和科研助理...