只需学习Kotlin,在下面的第一个代码中,其他代码中没有val
关键字,
如果省略了val
和var
,有什么区别?
class Person(val firstName: String, val lastName: String) {
}
class Person(firstName: String, lastName: String) {
}
答案 0 :(得分:8)
如果在构造函数中省略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)
}
但它也适用于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
最终求值时,这些值仍然可用。
答案 1 :(得分:6)
如果省略val或var,则它们将不是属性,而是将参数传递给构造函数。在构造函数之外,您将无法使用它们。