You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java的BaseExpandedViewCreator转Kotlin报JVM签名冲突怎么解决

错误产生原因

Kotlin 中声明var属性时,编译器会自动为其生成对应公开的setter、getter方法。你定义的var rawNotification: RawNotification ? = null会自动生成签名为setRawNotification(Lcom/myproject/RawNotification;)V的 setter 方法,和你手动编写的、用来实现NotificationViewCreator接口的setRawNotification方法的JVM签名完全一致,JVM 无法区分两个方法,因此抛出声明冲突错误。

除此之外你的代码还有三处和原Java逻辑不符的问题:

  • rawNotification原Java定义为protected可见性,你的Kotlin代码默认是public可见性
  • initRawNotification原Java定义为protected可重写,你的Kotlin代码默认是public且final不可重写
  • 接口实现方法没有加override关键字标记
解决方法

调整属性定义,关闭自动生成的公开setter,同时补全缺失的关键字即可,修改后的完整代码如下:

abstract class BaseExpandedViewCreator(
    protected val context: Context
) : NotificationViewCreator {

    // 属性可见性改为protected匹配原逻辑,同时私有化setter,避免生成公开set方法冲突
    protected var rawNotification: RawNotification? = null
        private set

    // 补全override关键字标记为接口实现方法
    override fun setRawNotification(rawNotification: RawNotification) {
        this.rawNotification = rawNotification
        initRawNotification(rawNotification)
    }

    // 补全protected和open关键字,允许子类重写匹配原Java逻辑
    protected open fun initRawNotification(rawNotification: RawNotification) {}
}

内容的提问来源于stack exchange,提问作者Kartik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 00:39:02