Kotlin 中的 Getters 和 Setters 是否应该与 Android 中的 Java 相同

Should Getters and Setters in Kotlin be the same as Java in Android

此模型 class 的 getter 和 setter 实现与 Java 中相同,但现在用 Kotlin 编写

 class HousePost{
private var uid: String = ""
private var postImage: String = ""
private var rent: String = ""
private var description: String = ""
private var publisher: String = ""
private var location: String = ""
private var postId: String? = ""
private var rooms: String = ""
private var caption: String = ""
private var dateTime: String = ""
var expandable: Boolean = false

constructor()
constructor(
   uid: String,
   postImage: String,
   rent: String,
   description: String,
   location: String,
   postId: String,
   publisher: String,
   rooms: String,
   caption: String,
   dateTime : String
) {
   this.uid = uid
   this.postImage = postImage
   this.rent = rent
   this.description = description
   this.location = location
   this.postId = postId
   this.publisher = publisher
   this.rooms = rooms
   this.caption = caption
   this.dateTime = dateTime
   this.expandable = false
}
//Post Details getters
fun getUid() : String {
   return uid
}
fun getPostId (): String? {
   return postId
}
fun getPostImage():String {
   return postImage
}
fun getDescription():String {
   return description
}
fun getLocation():String {
   return location
}
fun getRent():String {
   return rent
}
fun getPublisher(): String {
   return publisher
}

fun getRooms(): String {
   return rooms
}
fun getCaption() : String {
   return caption
}
fun getDateTime() : String {
   return dateTime
}

//Post Details setters
fun setUid (uid: String) {
   this.uid = uid
}
fun setPostId(postId: String) {
   this.postId = postId
}
fun setPostImage(postImage: String) {
   this.postImage = postImage
}
fun setLocation(location: String) {
   this.location = location
}
fun setDescription(description: String) {
   this.description = description
}
fun setRent(rent: String) {
   this.rent = rent
}
fun setPublisher(publisher: String) {
   this.publisher = publisher
}
fun setCaption(caption: String) {
   this.caption = caption
}
fun setDateTime(dateTime: String) {
   this.dateTime = dateTime
}

}

这个 class 应该对来自我的 database 的数据建模。这是 Kotlin 的正确方法吗? 我收到 No setter/field for class found 错误。我的 recyclerview 根本不显示数据,我认为是由于错误造成的。

你只要简单的做到这一点。去掉功能,不能用了

class HousePost {
    private var uid: String = ""
    private var postImage: String = ""
    private var rent: String = ""
    private var description: String = ""
    private var publisher: String = ""
    private var location: String = ""
    private var postId: String? = ""
    private var rooms: String = ""
    private var caption: String = ""
    private var dateTime: String = ""
    var expandable: Boolean = false

    constructor()
    constructor(uid: String,
                postImage: String,
                rent: String,
                description: String,
                location: String,
                postId: String,
                publisher: String,
                rooms: String,
                caption: String,
                dateTime: String) {
        this.uid = uid
        this.postImage = postImage
        this.rent = rent
        this.description = description
        this.location = location
        this.postId = postId
        this.publisher = publisher
        this.rooms = rooms
        this.caption = caption
        this.dateTime = dateTime
        this.expandable = false
    }
}

在 Kotlin 中,属性 不需要明确的 getter 或 setter 方法。

Properties 在 Kotlin classes 中可以使用 var 关键字声明为可变的,或者使用 val 关键字声明为 read-only .

A class 其主要目的是在 Kotlin 中保存数据,这些称为 data classes 并标有 data。 例如,在您的情况下,class 可以定义如下:

data class HousePost(
    val uid: String = ""
    val postImage: String = ""
    val rent: String = ""
    val description: String = ""
    val publisher: String = ""
    val location: String = ""
    val postId: String? = ""
    val rooms: String = ""
    val caption: String = ""
    val dateTime: String = ""
    val expandable: Boolean = false
)

val 关键字使此数据的属性 class 不可变。这意味着你不能在初始化后改变它的属性。

要使 属性 可变,您可以使用 var.

例如:

val housePost = HousePost() // This will use all the default value
housePost.expandable = true // Val cannot be reassigned

要使可扩展可变使用 var,如下所示:

data class HousePost(
    .
    .
    .
    var expandable: Boolean = false
)

val housePost = HousePost()
housePost.expandable = true // Can be reassigned

编辑

您遇到错误(没有 setter/field for class found error)因为您将 class 属性 标记为私有。您可以通过删除当前 class.

中的 private、构造函数、getters 和 setters 来简单地修复它

还有其他方法可以在 class 中定义 属性。您将在学习过程中了解如何使用它们。

class HousePost {
    var uid: String = ""
    var postImage: String = ""
    var rent: String = ""
    var description: String = ""
    var publisher: String = ""
    var location: String = ""
    var postId: String? = ""
    var rooms: String = ""
    var caption: String = ""
    var dateTime: String = ""
    var expandable: Boolean = false
}

或者这个:

class HousePost (
    var uid: String = ""
    var postImage: String = ""
    var rent: String = ""
    var description: String = ""
    var publisher: String = ""
    var location: String = ""
    var postId: String? = ""
    var rooms: String = ""
    var caption: String = ""
    var dateTime: String = ""
    var expandable: Boolean = false
)

Kotlin为properties提供了默认的getterssetters,您不必手动定义它们。

在科特林中

var data: String = ""

等同于

var data: String = ""
    get() = field
    set(value) {
        field = value
    }

因为 Kotlin 编译器会为您生成它。但是如果你只想让你的 getter public 而不是 setter,那么你可以覆盖 setter

var data: String = ""
    private set
    

您也可以对 getter 执行相同的操作。


This class is supposed to model data from my database. Is that the correct way in Kotlin?

不,这不是在 Kotlin 中定义模型的正确方法 class。 Kotlin提供了dataclasses,主要作用是保存数据。 Data class 主构造函数至少需要一个参数。

所以,你的模型class可以像这样重构

data class HousePost(
    var uid: String = "",
    var postImage: String = "",
    var rent: String = "",
    var description: String = "",
    var publisher: String = "",
    var location: String = "",
    var postId: String? = "",
    var rooms: String = "",
    var caption: String = "",
    var dateTime: String = "",
    var expandable: Boolean = false
)

无论如何,如果您只需要任何字段的私有设置器,您可以制作 constructor 属性 private,然后制作 public 属性 委托给这个私有字段。

data class HousePost(
    private var _uid: String = "",
    var postImage: String = "",
    var rent: String = "",
    var description: String = "",
    var publisher: String = "",
    var location: String = "",
    var postId: String? = "",
    var rooms: String = "",
    var caption: String = "",
    var dateTime: String = "",
    var expandable: Boolean = false
) {
    val uid: String get() = _uid
}