(翻譯)
學習Kotlin——lateinit vs lazy
lateinit vs lazy
在Kotlin中有許多優(yōu)秀的特性迄埃,我們可以利用所有這些特性在Kotlin中編寫更好的應用程序棉姐。在所有這些特性中妄均,lateinit和lazy是重要的屬性初始化特性荐类。我們必須知道何時使用哪個屬性初始化境肾。
Kotlin 屬性初始化
如果您不想在構造函數(shù)中初始化屬性蚂夕,那么這是Kotlin中屬性初始化的兩種重要方法。
- lateinit
- lazy
lateinit
lateinit是后期初始化佩谷。
通常旁壮,聲明為非空類型的屬性必須在構造函數(shù)中初始化。然而谐檀,這通常是不方便的寡具。例如,可以通過依賴項注入或在單元測試的設置方法中初始化屬性稚补。在這種情況下,您不能在構造函數(shù)中提供非空初始化器框喳,但在引用類體內(nèi)的屬性時课幕,仍然希望避免空檢查。
要處理這種情況五垮,可以使用lateinit修飾符標記屬性乍惊。
例子
public class Test {
lateinit var mock: Mock
@SetUp fun setup() {
mock = Mock()
}
@Test fun test() {
mock.do()
}
}
修飾符只能用于類主體中聲明的var屬性(不在主構造函數(shù)中),并且只有在屬性沒有自定義的getter或setter時才可以使用放仗。屬性的類型必須是非空的润绎,并且不能是原始類型。
lazy
lazy 就是延遲初始化。
lazy()是一個接受lambda并返回一個lazy實例的函數(shù)莉撇,該實例可以作為實現(xiàn)lazy屬性的委托:對get()的第一個調(diào)用執(zhí)行傳遞給lazy()的lambda并記住結果呢蛤,對get()的后續(xù)調(diào)用只返回記住的結果。
例子
public class Example{
val name: String by lazy { “Amit Shekhar” }
}
所以第一次調(diào)用和隨后的調(diào)用棍郎,名字會返回" Amit Shekhar "
現(xiàn)在其障,如何選擇何時使用哪一個?
- lazy只能用于val屬性,而lateinit只能應用于vars涂佃,因為它不能編譯成final字段励翼,因此不能保證不變性。
- lateinit變量可以在任何對象被看到的地方初始化辜荠。如果您希望以一種可能事先未知的方式從外部初始化您的屬性汽抚,請使用lateinit。
(原文)
Learn Kotlin?—? lateinit vs lazy
lateinit vs lazy
There are many great features available in Kotlin, we can take advantage of all these features to write the better application in Kotlin. Among all those features, lateinit and lazy are important property initialization feature. We must know when to use which property initialization.
Kotlin Property Initialization
If you do not want to initialize a property in the constructor, then these are two important ways of property initialisation in Kotlin.
- lateinit
- lazy
lateinit
lateinit is late initialization.
Normally, properties declared as having a non-null type must be initialized in the constructor. However, fairly often this is not convenient. For example, properties can be initialized through dependency injection, or in the setup method of a unit test. In this case, you cannot supply a non-null initializer in the constructor, but you still want to avoid null checks when referencing the property inside the body of a class.
To handle this case, you can mark the property with the lateinit modifier.
Example
public class Test {
lateinit var mock: Mock
@SetUp fun setup() {
mock = Mock()
}
@Test fun test() {
mock.do()
}
}
The modifier can only be used on var properties declared inside the body of a class (not in the primary constructor), and only when the property does not have a custom getter or setter. The type of the property must be non-null, and it must not be a primitive type.
lazy
lazy is lazy initialization.
lazy() is a function that takes a lambda and returns an instance of lazy which can serve as a delegate for implementing a lazy property: the first call to get() executes the lambda passed to lazy() and remembers the result, subsequent calls to get() simply return the remembered result.
Example
public class Example{
val name: String by lazy { “Amit Shekhar” }
}
So the first call and the subsequent calls, name will return “Amit Shekhar”
Now, How to choose when to use which one?
- lazy can only be used for val properties, whereas lateinit can only be applied to vars because it can’t be compiled to a final field, thus no immutability can be guaranteed.
- lateinit var can be initialized from anywhere the object is seen from. If you want your property to be initialized from outside in a way probably unknown beforehand, use lateinit.
原文鏈接 https://blog.mindorks.com/learn-kotlin-lateinit-vs-lazy