kotlin 通用中的房间实体类

时间:2021-01-16 21:21:27

标签: android kotlin generics android-room

我试图在 kotlin 中创建一个通用类,我的通用属性应该是 Room Library 的实体(或数据类,因为我的实体是数据类)。

@Entity()
data class MyEntity(
    @PrimaryKey
    var id: Int? = null,
    val title: String,
)

class MyGenericClass<T : isEntity>() {
    // This is the important part

}

我怎样才能刺穿它?

1 个答案:

答案 0 :(得分:0)

无法在 Kotlin 泛型类型上放置“用 X 注释”的约束。如果您可以控制实体类,只需创建一个接口,让您的所有实体都实现它,并将其用作通用约束:

interface BaseEntity

@Entity
data class MyEntity(...) : BaseEntity

class GenericClass<T : BaseEntity> {
    // ...
}
相关问题