只是学习 Kotlin 在下面的第一个代码中,在另一个代码中有一个val
关键字,没有, 如果省略val
和var
,这里有什么不同?
class Person(val firstName: String, val lastName: String) {
}
class Person(firstName: String, lastName: String) {
}
如果省略 val 或 var,则它们将不是属性,而是传递给构造函数的参数。您将无法在构造函数之外使用它们。
如果在构造函数中省略val
或var
,则唯一可以访问这些参数的地方是在构造时计算的初始化语句。见 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)
}
但它也适用于使用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
最终计算时,这些值仍然可用。