this
报告不安全的操作,包括:
this
作为函数参数如果其他类继承自给定的类,则在执行不安全操作时,可能无法完全初始化它们。
示例:
abstract class Base {
val code = calculate()
abstract fun calculate(): Int
}
class Derived(private val x: Int) : Base() {
override fun calculate() = x
}
fun testIt() {
println(Derived(42).code) // 预期值:42,实际值:0
}