我对我的 kotlin 课程没有按预期工作有点困惑:
用于检查更新信息的数据类:
data class UpdateInfo constructor(//kotlin class
val description: String,
val force: Int,
val platform: String,
val title: String,
val url: String,
@SerializedName("version")
val versionCode: Int = 0
) : Serializable {
val isForceUpdate = force == 1
}
也是一个用于解码对象形式 json 的工具:
public class JsonUtil {//java class
private static final Gson gson;
static {
BooleanAdapter booleanAdapter = new BooleanAdapter();
gson = new GsonBuilder()
.serializeNulls()
.disableHtmlEscaping()
.setLenient()
.registerTypeAdapter(Boolean.class, booleanAdapter)
.registerTypeAdapter(boolean.class, booleanAdapter)
.create();
}
}
当我测试它时:
val json = "{\"force\"=\"1\"}"
val info = JsonUtil.fromJsonObject(json, UpdateInfo::class.java)
println(info)
println(info.force == 1)
println(info.isForceUpdate)
我得到:
UpdateInfo(description=null, force=1, platform=null, title=null, url=null,versionCode=0)
true
false
什么??info.isForceUpdate = 假???
然后我尝试了lateinit
or by lazy{}
,仍然无法正常工作。所以,我该怎么办..我info.force==1
现在直接使用,但我仍然想知道为什么会这样。