Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
锁定Kotlin中的serialVersionUID_Kotlin - Fatal编程技术网

锁定Kotlin中的serialVersionUID

锁定Kotlin中的serialVersionUID,kotlin,Kotlin,我整个上午都在努力把serialVersionUID锁在Kotlin的课堂上。我有一个BaseModel,它由Project abstract class BaseModel<T>( var id: Int? = null, private val fileName: String, private val data: MutableList<T>, private val indices: MutableM

我整个上午都在努力把
serialVersionUID
锁在Kotlin的课堂上。我有一个
BaseModel
,它由
Project

abstract class BaseModel<T>(
        var id: Int? = null,
        private val fileName: String,
        private val data: MutableList<T>,
        private val indices: MutableMap<Int, T>
) : Serializable {

  ...

   protected fun writeToDisk() {
       val oos = ObjectOutputStream(BufferedOutputStream(FileOutputStream(fetchFileName()))   )
       oos.writeObject(fetchData());
       oos.close();
   }


}
对所有班级都没有影响

关于StackOverflow的一些示例使用了
serialVersionUid
,这也没有任何效果(我认为这是出于某种原因将最后两个字母的小写)


@JvmStatic
在这里不起作用,因为它不是
对象
,我尝试将其设置为非私有对象,但没有成功。

解决方案实际上比我想象的简单得多,使用一个伴生对象。现在它可以完美地序列化,如果我添加更多字段,它仍然会序列化到磁盘并反序列化,除非我更改
serialVersionUID

基数:

抽象类基模型(
变量id:Int?=null,
private val fileName:String,
私有val数据:可变列表,
私有val索引:可变映射
):可序列化{
伴星{
@JvmStatic私有val serialVersionUID:Long=1
}
...
}
项目:

class Project(
        var name: String = "",
        var repo: String = ""

) : BaseModel<Project>(
        data = Data.projects,
        indices = Data.projectsIndex,
        fileName = "data/projects.dat"
), Serializable {

    companion object {
        @JvmStatic private val serialVersionUID: Long = 1
    }

    override fun toString(): String {
        return "Project: id=${id}, name=${name}, repo=${repo}"
    }

}
类项目(
变量名称:String=“”,
var repo:String=“”
):BaseModel(
data=data.projects,
索引=Data.projectsIndex,
fileName=“data/projects.dat”
),可序列化{
伴星{
@JvmStatic私有val serialVersionUID:Long=1
}
重写funtostring():String{
return“Project:id=${id},name=${name},repo=${repo}”
}
}

您可以将
serialVersionUID
定义为伴生对象中的常量:

abstract class BaseModel<T> : Serializable {
    companion object {
        private const val serialVersionUID: Long = -1
    }
}
抽象类基模型:可序列化{
伴星{
private const val serialVersionId:Long=-1
}
}
常量被编译为字段,而伴星的字段被存储为包含伴星的类的静态字段。因此,您可以得到所需的–在可序列化类中有一个私有静态字段
serialVersionUID

安装此插件:,使用插件自动生成默认串行版本uid,用法:。

现在记录的可能重复项:
abstract class BaseModel<T>(
        var id: Int? = null,
        private val fileName: String,
        private val data: MutableList<T>,
        private val indices: MutableMap<Int, T>
) : Serializable {

    companion object {
        @JvmStatic private val serialVersionUID: Long = 1
    }

    ...

}
class Project(
        var name: String = "",
        var repo: String = ""

) : BaseModel<Project>(
        data = Data.projects,
        indices = Data.projectsIndex,
        fileName = "data/projects.dat"
), Serializable {

    companion object {
        @JvmStatic private val serialVersionUID: Long = 1
    }

    override fun toString(): String {
        return "Project: id=${id}, name=${name}, repo=${repo}"
    }

}
abstract class BaseModel<T> : Serializable {
    companion object {
        private const val serialVersionUID: Long = -1
    }
}