【问题标题】:How to pass argument that inherits base class and implements interface? [duplicate]如何传递继承基类并实现接口的参数? [复制]
【发布时间】:2019-08-20 14:21:00
【问题描述】:

我需要在方法中传递一个对象,该类从基类继承并实现我的接口。我在下面用伪 Kotlin 语法写了它:

fun destroyBrains(justKillMe: BaseClass & IListener) {
}

在 Kotlin 中可以吗? (我的意思是不引入一个将实现BaseClassIListener 的新类)

【问题讨论】:

  • @MadhuBhat,我明白了,谢谢你的回答

标签: kotlin


【解决方案1】:

您可以使用where 子句为泛型类型设置多个上限:

fun <T> destroyBrains(justKillMe: T)
    where T : BaseClass,
          T : IListener {

}

根据您要实现的目标,也许这会很有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-06
    • 2018-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-25
    • 2023-03-14
    相关资源
    最近更新 更多