5
我已經使用android插件的android工作室自動生成的功能,它生成了以下代碼,但我不明白爲什麼需要final val
爲CREATOR
字段?我在kotlin首次看到final
關鍵字。Kotlin android parcelable
data class Person(
val name: String,
val surname: String
) : Parcelable {
constructor(source: Parcel): this(source.readString(), source.readString())
override fun describeContents(): Int {
return 0
}
override fun writeToParcel(dest: Parcel?, flags: Int) {
dest?.writeString(name)
dest?.writeString(surname)
}
companion object {
@JvmField final val CREATOR: Parcelable.Creator<Person> = object : Parcelable.Creator<Person> {
override fun createFromParcel(source: Parcel): Person {
return Person(source)
}
override fun newArray(size: Int): Array<Person?> {
return arrayOfNulls(size)
}
}
}
}