我在Kotlin数据类中声明了10+变量,我想为它创建一个空构造函数,就像我们通常在Java中做的那样。
数据类:
data class Activity(
var updated_on: String,
var tags: List<String>,
var description: String,
var user_id: List<Int>,
var status_id: Int,
var title: String,
var created_at: String,
var data: HashMap<*, *>,
var id: Int,
var counts: LinkedTreeMap<*, *>,
)
预期用途:
val activity = Activity();
activity.title = "New Computer"
sendToServer(activity)
但是data类要求在创建构造函数时传递所有参数。我们如何像Java POJO类构造函数那样简化它?
val activity = Activity(null,null,null,null,null,"New Computer",null,null,null,null)
sendToServer(activity)
如果你给每个主构造函数参数一个默认值:
data class Item(var id: String = "",
var title: String = "",
var condition: String = "",
var price: String = "",
var categoryId: String = "",
var make: String = "",
var model: String = "",
var year: String = "",
var bodyStyle: String = "",
var detail: String = "",
var latitude: Double = 0.0,
var longitude: Double = 0.0,
var listImages: List<String> = emptyList(),
var idSeller: String = "")
从课堂上的实例中你可以不带参数地调用它或者用你当时的参数
var newItem = Item()
var newItem2 = Item(title = "exampleTitle",
condition = "exampleCondition",
price = "examplePrice",
categoryId = "exampleCategoryId")
我建议修改主构造函数,并为每个形参添加默认值:
data class Activity(
var updated_on: String = "",
var tags: List<String> = emptyList(),
var description: String = "",
var user_id: List<Int> = emptyList(),
var status_id: Int = -1,
var title: String = "",
var created_at: String = "",
var data: HashMap<*, *> = hashMapOf<Any, Any>(),
var id: Int = -1,
var counts: LinkedTreeMap<*, *> = LinkedTreeMap<Any, Any>()
)
您还可以通过添加?然后你可以调用null:
data class Activity(
var updated_on: String? = null,
var tags: List<String>? = null,
var description: String? = null,
var user_id: List<Int>? = null,
var status_id: Int? = null,
var title: String? = null,
var created_at: String? = null,
var data: HashMap<*, *>? = null,
var id: Int? = null,
var counts: LinkedTreeMap<*, *>? = null
)
一般来说,避免可空对象是一个很好的实践——以我们不需要使用它们的方式编写代码。与Java相比,非空对象是Kotlin的优点之一。因此,上面的第一个选项更可取。
这两种选择都会给你想要的结果:
val activity = Activity()
activity.title = "New Computer"
sendToServer(activity)
如果你给每个主构造函数参数一个默认值:
data class Item(var id: String = "",
var title: String = "",
var condition: String = "",
var price: String = "",
var categoryId: String = "",
var make: String = "",
var model: String = "",
var year: String = "",
var bodyStyle: String = "",
var detail: String = "",
var latitude: Double = 0.0,
var longitude: Double = 0.0,
var listImages: List<String> = emptyList(),
var idSeller: String = "")
从课堂上的实例中你可以不带参数地调用它或者用你当时的参数
var newItem = Item()
var newItem2 = Item(title = "exampleTitle",
condition = "exampleCondition",
price = "examplePrice",
categoryId = "exampleCategoryId")
除了@miensol的答案,让我添加一些细节:
如果希望使用数据类获得java可见的空构造函数,则需要显式地定义它。
使用默认值+构造函数说明符非常简单:
data class Activity(
var updated_on: String = "",
var tags: List<String> = emptyList(),
var description: String = "",
var user_id: List<Int> = emptyList(),
var status_id: Int = -1,
var title: String = "",
var created_at: String = "",
var data: HashMap<*, *> = hashMapOf<Any, Any>(),
var id: Int = -1,
var counts: LinkedTreeMap<*, *> = LinkedTreeMap<Any, Any>()
) {
constructor() : this(title = "") // this constructor is an explicit
// "empty" constructor, as seen by Java.
}
这意味着通过这个技巧,您现在可以使用标准Java序列化器(Jackson、Gson等)序列化/反序列化这个对象。