相关疑难解决方法(0)

Kotlin的吸气剂和二传手

例如,在Java中,我可以自己编写getter(由IDE生成)或者在lombok中使用@Getter之类的注释 - 这非常简单.

然而,Kotlin 默认拥有getter和setter.但我无法理解如何使用它们.

我想说,就像Java一样:

private val isEmpty: String
        get() = this.toString() //making this thing public rises an error: Getter visibility must be the same as property visibility.
Run Code Online (Sandbox Code Playgroud)

那么吸气剂如何工作?

getter-setter kotlin

59
推荐指数
6
解决办法
5万
查看次数

在kotlin中,如何使主构造函数中的属性setter成为私有?

在kotlin中,如何使主构造函数中的属性setter成为私有?

class City(val id: String, var name: String, var description: String = "") {

    fun update(name: String, description: String? = "") {
        this.name = name
        this.description = description ?: this.description
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望属性的setter name是私有的,并且公开的getter,我该怎么办?

constructor kotlin

10
推荐指数
1
解决办法
2783
查看次数

标签 统计

kotlin ×2

constructor ×1

getter-setter ×1