我有java接口(interface)
public interface LifecycleRegistryOwner extends LifecycleOwner {
@Override
LifecycleRegistry getLifecycle();
}
和实现这个接口(interface)的 Kotlin 类,如果有
val lifecycle by lazy {
val result = LifecycleRegistry(this)
result
}
但编译器说这是一个意外的覆盖。有没有办法说它是故意的?
我目前的实现
class PartnerSettingsActivity :AppCompatActivity(),LifecycleRegistryOwner {
private val registry = LifecycleRegistry(this) //<< hate this class-wide val
override fun getLifecycle(): LifecycleRegistry = registry
最佳答案
您不能告诉 Kotlin 编译器接受这种“意外覆盖”的风险,因为在字节码级别会有冲突的构造。你也不能使用@JvmName修改字节码中生成的字段的名称,因为它不适用于委托(delegate),而惰性是委托(delegate)。
关于interface - 不是 Kotlin 中的意外覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46152134/