[英]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
提供了默认的getters
和setters
,您不必手动定义它们。
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.