I have some code that I've been using for years in Java, but need it in Kotlin (its interfaces extend collections interfaces). Some classes are serializable. When I try the obvious, I get "Cannot access 'Serializable': it is internal in kotlin.io":

class Foo(val someField:Int): Serializable {
    companion object {
        private const val serialVersionUID = 20180617104400L
    }
}

So, do I just import java.io.Serializable, or will that cause other issues?

推荐答案

do I just import java.io.Serializable

Yes. Just be aware that Kotlin uses @Transient annotation instead of a keyword.

Of course, Java serialization does have its issues, but there's no difference in that respect between Kotlin and Java, and if you are happy with your current code...

Kotlin相关问答推荐

如何接受任何派生类KClass

在 Kotlin 中实现并输入 Either

如果带注释的成员未被特定块包围,则发出 IDE 警告

Kotlin 中的as Long和.toLong()有什么区别?

如何在 kotlin 中创建自定义迭代器并添加到现有类?

为什么没有remember 的 mutableStateOf 有时会起作用?

Saripaar formvalidation 在 kotlin 中第二次不起作用

Kotlin:泛型、反射以及类型 T 和 T:Any 之间的区别

DatabaseManager_Impl 不是抽象的,不会覆盖 RoomDatabase 中的抽象方法 clearAllTables()

如何通过反射使用 Kotlin 对象

Kotlin通过映射委托属性,如果映射中不存在,则抛出NoTouchElementException

Kotlin Native如何将字节数组转换为字符串?

如何在 Jetpack Compose 的 LazyColumn/LazyRow 中禁用和启用滚动?

Kotlin中的Memoization功能

Kotlin - 具有私有构造函数的类的工厂函数

什么是 Kotlin 等价于 Class<?>

如何在伴随对象中使用泛型

项目不会使用 Kotlin 1.1.3 构建

从 Kotlin 访问 Integer.class

Kotlin:如何使用扩展函数扩展枚举类