我在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)
Kotlin中数据类的非空辅助构造函数:
data class ChemicalElement(var name: String,
var symbol: String,
var atomicNumber: Int,
var atomicWeight: Double,
var nobleMetal: Boolean?) {
constructor(): this("Silver", "Ag", 47, 107.8682, true)
}
fun main() {
var chemicalElement = ChemicalElement()
println("RESULT: ${chemicalElement.symbol} means ${chemicalElement.name}")
println(chemicalElement)
}
// RESULT: Ag means Silver
// ChemicalElement(name=Silver, symbol=Ag, atomicNumber=47,
// atomicWeight=107.8682, nobleMetal=true)
Kotlin中数据类的空辅助构造函数:
data class ChemicalElement(var name: String,
var symbol: String,
var atomicNumber: Int,
var atomicWeight: Double,
var nobleMetal: Boolean?) {
constructor(): this("", "", -1, 0.0, null)
}
fun main() {
var chemicalElement = ChemicalElement()
println(chemicalElement)
}
// ChemicalElement(name=, symbol=, atomicNumber=-1,
// atomicWeight=0.0, nobleMetal=null)
Kotlin中数据类的非空辅助构造函数:
data class ChemicalElement(var name: String,
var symbol: String,
var atomicNumber: Int,
var atomicWeight: Double,
var nobleMetal: Boolean?) {
constructor(): this("Silver", "Ag", 47, 107.8682, true)
}
fun main() {
var chemicalElement = ChemicalElement()
println("RESULT: ${chemicalElement.symbol} means ${chemicalElement.name}")
println(chemicalElement)
}
// RESULT: Ag means Silver
// ChemicalElement(name=Silver, symbol=Ag, atomicNumber=47,
// atomicWeight=107.8682, nobleMetal=true)
Kotlin中数据类的空辅助构造函数:
data class ChemicalElement(var name: String,
var symbol: String,
var atomicNumber: Int,
var atomicWeight: Double,
var nobleMetal: Boolean?) {
constructor(): this("", "", -1, 0.0, null)
}
fun main() {
var chemicalElement = ChemicalElement()
println(chemicalElement)
}
// ChemicalElement(name=, symbol=, atomicNumber=-1,
// atomicWeight=0.0, nobleMetal=null)
我建议修改主构造函数,并为每个形参添加默认值:
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)
除了@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等)序列化/反序列化这个对象。