1.延迟属性
(1)概念
属性通过lazy()函数返回Lazy作为委托对象实现延迟初始化,返回的Lazy对象实现了getValue符合只读委托属性的方法,因此可作为只读属性的委托对象。
LazyJVM.kt部分源码:
lazy(initializer: () -> T)方法传入一个lambda表达式返回需要委托的属性初始值,并返回委托对象Lazy。
/**
* Creates a new instance of the [Lazy] that uses the specified initialization function [initializer]
* and the default thread-safety mode [LazyThreadSafetyMode.SYNCHRONIZED].
*
* If the initialization of a value throws an exception, it will attempt to reinitialize the value at next access.
*
* Note that the returned instance uses itself to synchronize on. Do not synchronize from external code on
* the returned instance as it may cause accidental deadlock. Also this behavior can be changed in the future.
*/
public actual fun <T> lazy(initiali