[英]How to call Java/Kotlin GCP Cloud Functions from Android
[英]Should Getters and Setters in Kotlin be the same as Java in Android
这个 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
}
}
这个 class 应该是我数据库中的 model 数据。 这是 Kotlin 中的正确方法吗? 我遇到了一个No setter/field for class found错误。 我的 recyclerview 根本不显示数据,我认为是由于错误造成的。
在 Kotlin 中,属性不需要显式的 getter 或 setter 方法。
Kotlin 类中的属性既可以使用var
关键字声明为可变的,也可以使用val
关键字声明为只读的。
一个 class 其主要目的是在 Kotlin 中保存数据这些称为数据类并用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
编辑
您收到错误(找不到 class 的设置器/字段错误),因为您将 class 属性标记为私有。 您可以通过删除当前 class 中的 private、构造函数、getter 和 setter 来简单地修复它。
在 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
)
你只是简单地做到这一点。 删除功能,不能用
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 为properties
提供了默认的getters
和setters
,您不必手动定义它们。
Kotlin
var data: String = ""
相当于
var data: String = ""
get() = field
set(value) {
field = value
}
因为 Kotlin 编译器会为您生成它。 但是如果你只想让你的getter
public
而不是setter
,那么你可以覆盖setter
var data: String = ""
private set
你也可以为getter
做同样的事情。
这个 class 应该是我数据库中的 model 数据。 这是 Kotlin 中的正确方法吗?
不,那不是在Kotlin中定义一个model class的正确方法。Kotlin提供data
类,其主要function是保存数据的。 Data class
主构造函数至少需要一个参数。
所以,你的 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
)
在任何情况下,如果您只需要任何字段的私有设置器,您可以将该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.