小编Mat*_*her的帖子

Kotlin:使用不同的子类多次实现泛型接口(基类是约束)

错误消息:“ScheduableInserter”的类型参数 E 具有不一致的值:事件、分配

所以基本上我有一个抽象类Schedulable。此外,我有两个类事件(单个发生的事件,如时间戳)和一个类分配(基本上是一个事件,但也有持续时间)我有一个 MainActivity,我想在其中将实例插入 SQLiteDB。我想为每个 Schedulable 子类使用不同的插入方法来轻松处理它们之间的差异。

//ScheduableInserter.kt

interface SchedulableInserter<E> where E : Schedulable{
        fun onInsertSchedulable(item:E)
}
Run Code Online (Sandbox Code Playgroud)

//主活动.kt

class MainActivity() : AppCompatActivity(),
    NavigationView.OnNavigationItemSelectedListener,
    SchedulableInserter<Incident>, SchedulableInserter<Assignment> {
 /* ^                                ^
    |                                |
   Sadly Kotlin says:
   Type parameter E of 'SchedulableInserter' has inconsistent values: 
   Incident, Assignment
   A Supertype apperars twice
*/
}
Run Code Online (Sandbox Code Playgroud)

我的意思是,如果我将 SchedulerInserter 绑定到 Schedulable,我可以在方法中进行区分,但我希望不必这样做。

generics android constraints kotlin

3
推荐指数
1
解决办法
1360
查看次数

Kotlin接口属性:仅要求公共获取者,而无需公共设置者

我试图创建一个接口,以强制其派生类具有一定的属性,但只能使用公共获取程序。由于我不想在其私有范围之外进行更改,因此我不希望它实现公共集。

interface Transaction{
   var transferDate: Date get //I only require a get, no set
}

class MoneyTransaction(){
   override var transferDate: Date get private set 
/*private set does not work*/
}
Run Code Online (Sandbox Code Playgroud)

scope properties interface set kotlin

2
推荐指数
1
解决办法
212
查看次数

标签 统计

kotlin ×2

android ×1

constraints ×1

generics ×1

interface ×1

properties ×1

scope ×1

set ×1