gpt4 book ai didi

kotlin - 这是正确的 “lateinit var text : String?”吗?

转载 作者:行者123 更新时间:2023-12-02 12:38:16 25 4
gpt4 key购买 nike

我需要知道此行代码是否正确,我的老师告诉我这是正确的,但是我不同意“因为“lateinit”不能与可能为null的变量一起使用。
行代码:

    lateinit var text : String?

码:
    val cadena = null
lateinit var text : String?
text = null
text = cadena ?: "Hola"
text?.let { println(text) }

最佳答案

你是正确的,你的老师是错误的。证明:lateinit var text : String?使用Kotlin 1.3.50导致编译错误:

'lateinit' modifier is not allowed on properties of nullable types



任何老师怎么可能声称这样的代码是正确的,这超出了我...

关于kotlin - 这是正确的 “lateinit var text : String?”吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58479888/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com