【问题标题】:Kotlin how to create anonymous class that implements an interface with 2 methods?Kotlin 如何创建实现具有 2 个方法的接口的匿名类?
【发布时间】:2019-08-07 19:35:18
【问题描述】:

我有两个方法的接口:

interface Human {
   fun talk()
   fun think()
}

我想将此接口的匿名实例传递给方法。

我该怎么做?

【问题讨论】:

标签: inheritance kotlin


【解决方案1】:

如果foo 看起来像这样:

fun foo(human: Human) {
    // ...
}

您可以像这样调用它,使用 object expression:

foo(object: Human {
    override fun think() {
        // ...
    }

    override fun talk() {
        //...
    }
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-06
    • 1970-01-01
    • 2016-10-06
    • 2017-11-02
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    相关资源
    最近更新 更多