【发布时间】:2017-03-12 11:42:27
【问题描述】:
这是一个演示问题的最小示例:
abstract class Base {
abstract fun String.extension(x: Char)
}
class Derived : Base() {
override fun String.extension(x: Char) {
// Calling lots of methods on String, hence extension method
println("${first()} $length ${last()} ${firstOrNull { it == x }} ...")
}
}
从 Java 调用扩展方法很简单:
Base o = new Derived();
o.extension("hello world", 'l');
但我不知道如何在纯 Kotlin 中做到这一点。 String 和 Base 似乎都没有 extension 方法。
【问题讨论】: