【发布时间】:2017-06-28 07:25:42
【问题描述】:
我对 lateinit 和可为空的变量感到困惑,哪一个用于变量。
lateinit var c: String
var d: String? = null
c = "UserDefinedTarget"
// if not added initialisation for c than throws UninitializedPropertyAccessException
if (c == "UserDefinedTarget") {
//do some stuff.
}
//not throws any exception whether d is initialise or not.
if(d == "UserDefinedTarget") {
//do some stuff
}
【问题讨论】:
-
如果您绝对确定变量会在使用前被初始化。您可以使用后期初始化。否则使用可为空的变量
-
for lateint add check if (this::c.isInitilized())