简体   繁体   English

Kotlin中的Getters和Setters应该和Android中的Java一样吗

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

This Model class has getters and setters implemented the same as in Java but now written in Kotlin这个 Model 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
}

} }

This class is supposed to model data from my database .这个 class 应该是我数据库中的 model 数据。 Is that the correct way in Kotlin?这是 Kotlin 中的正确方法吗? There is a No setter/field for class found error I'm getting.我遇到了一个No setter/field for class found错误。 My recyclerview does not show the data at all, I assume is due to the error.我的 recyclerview 根本不显示数据,我认为是由于错误造成的。

In Kotlin, a property doesn't require explicit getter or setter methods.在 Kotlin 中,属性不需要显式的 getter 或 setter 方法。

Properties in Kotlin classes can be declared either as mutable, using the var keyword, or as read-only, using the val keyword. Kotlin 类中的属性既可以使用var关键字声明为可变的,也可以使用val关键字声明为只读的。

A class whose main purpose is to hold data in Kotlin these are called data classes and are marked with data .一个 class 其主要目的是在 Kotlin 中保存数据这些称为数据类并用data标记。 For example in your case the class can be define as below:例如,在您的情况下, 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
)

The val keyword make the properties of this data class immutable. val关键字使此数据 class 的属性不可变。 It means that you cannot change its properties after initialized.这意味着您无法在初始化后更改其属性。

To make a property mutable you can use var .要使属性可变,您可以使用var

For example:例如:

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

To make expandable mutable use var like so:要使可扩展可变使用var ,如下所示:

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

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

Edit编辑

You got the error(No setter/field for class found error) because you mark the class property private.您收到错误(找不到 class 的设置器/字段错误),因为您将 class 属性标记为私有。 You can simply fix it by removing private, the constructor, getters and setters in your current class.您可以通过删除当前 class 中的 private、构造函数、getter 和 setter 来简单地修复它。

There are other ways to define a property in a class. You will get to know how to use them as you learn.在 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
}

Or this:或这个:

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
)

You just simple do this.你只是简单地做到这一点。 Remove the functions, it's not usable删除功能,不能用

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 provides default getters and setters for properties , you don't have to manually define them. Kotlin 为properties提供了默认的getterssetters ,您不必手动定义它们。

In Kotlin Kotlin

var data: String = ""

is equivalent as相当于

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

because Kotlin compiler generates it for you.因为 Kotlin 编译器会为您生成它。 But if you only want to make your getter public but not setter , then you can override setter但是如果你只想让你的getter public而不是setter ,那么你可以覆盖setter

var data: String = ""
    private set
    

You can do the same for the getter too.你也可以为getter做同样的事情。


This class is supposed to model data from my database.这个 class 应该是我数据库中的 model 数据。 Is that the correct way in Kotlin?这是 Kotlin 中的正确方法吗?

No, that is not the correct way to define a model class in Kotlin. Kotlin provides data classes, whose main function is to hold data.不,那不是在Kotlin中定义一个model class的正确方法。Kotlin提供data类,其主要function是保存数据的。 Data class primary constructor needs at least one parameter. Data class主构造函数至少需要一个参数。

So, your model class can be refactored like this所以,你的 model 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
)

In any case, if you need only private setters for any field, you can make that constructor property private , and then make a public property that delegates to this private field.在任何情况下,如果您只需要任何字段的私有设置器,您可以将该constructor属性设为private ,然后创建一个委托给该私有字段的公共属性。

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Android 调用 Java/Kotlin GCP Cloud Functions - How to call Java/Kotlin GCP Cloud Functions from Android 在 java/Kotlin/Android 中基于 TimeStamp 从 firestore 中获取数据 || 获取java中的TimeStamp与firebase中的TimeStamp进行比较 - Fetch data from firestore based on TimeStamp in java/Kotlin/Android || Get TimeStamp in java to compare with firebase TimeStamp Android Kotlin Firebase 如何同时检索两个图像并将它们添加到位图数组中? - Android Kotlin Firebase How to retrieve two image at the same time and add them into a bitmap array? Java 中从客户端应用程序隐藏公共设置器的最佳方法 - Best way in Java to hide public setters from client applications Firebase isEmailVerified 无效 kotlin android - Firebase isEmailVerified is not working kotlin android 为什么 getValue(nameClass::class.java) 在 Kotlin 和 Firebase 在 Android 为我工作 - why don't getValue(nameClass ::class.java) work for me in Kotlin with Firebase in Android Kotlin & Android Studio - 未解决的参考:FirebaseInstanceId - Kotlin & Android Studio - Unresolved reference: FirebaseInstanceId 使用 Android Kotlin 从 Firebase 获取数据 - fetching data from Firebase with Android Kotlin Kotlin 协程在 Android 应用程序中未完成时崩溃 - Crash when Kotlin Coroutine Not Complete in Android App 使用时出错 Firebase Android Studio (kotlin) - Error while using Firebase Android Studio (kotlin)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM