为什么要将 val 或 var 放在 kotlin class 构造函数中

Why to put val or var in kotlin class constructors

刚刚学习 Kotlin 在下面的第一个代码中有 val 关键字,而在其他代码中没有, 如果省略 valvar,这里有什么不同?

class Person(val firstName: String, val lastName: String) {
}

class Person(firstName: String, lastName: String) {
}

如果省略 val 或 var 则它们将不是属性,而是传递给构造函数的参数。您将无法在构造函数之外使用它们。

如果在构造函数中省略 valvar,那么唯一可以访问这些参数的地方是在构造时计算的初始化语句。参见 https://kotlinlang.org/docs/reference/classes.html

当您想在存储值之前对值执行某些操作时,这很有用。在 Java 中,您可以将该代码放入构造函数体

class Person(firstName: String, lastName: String) {
    // directly in val / var declarations
    val firstName = firstName.capitalize()
    val lastName = lastName

    // in init blocks
    val fullName: String
    init {
        fullName = "$firstName $lastName"
    }

    // secondary constructors can only see their own parameters
    // and nothing else can access those
    constructor(fullName: String) : this("", fullName)
}

但它也适用于 delegation using by

interface Named {
    fun getName(): String
}
class Human(private val fname: String, private val lname: String) : Named {
    override fun getName() = "$fname + $lname" // functions need val since
                                               // value is resolved after construction
}
class Person2(firstName: String, lastName: String) : Named by Human(firstName, lastName)

class Person3(human: Human) : Named by human {
    constructor(firstName: String, lastName: String): this(Human(firstName, lastName))
}

或在属性代表团中

class Person4(firstName: String, lastName: String) {
    val fullName: String by lazy { "$firstName $lastName" }
}

注意:闭包是在初始化时捕获的,因此当 lazy 最终计算时,这些值仍然可用。