Kotlin:平台声明冲突:相同的JVM签名

时间:2018-03-26 18:54:40

标签: kotlin

我正在尝试在kotlin中实现一个扩展MultiValuedMap的抽象类,当我试图覆盖keySet()方法时,我收到了错误

platform declaration clash: The following declarations have the same JVM signature (keySet()Ljava/util/Set;)

我的代码:

abstract class ConfigProperties<K, V>(delegate: Map<K, V>?): MultivaluedMap<String, String> {

  protected val delegate: Map<K, V>

  init {
      if (delegate == null) {
          throw NullPointerException("Config properties delegate must not be null.")
      }
      this.delegate = delegate
  }

  abstract fun putCacheProperty(key: Parameter, value: Any)

  abstract fun getCacheProperty(key: Parameter): Any

  protected val UNSUPPORTED_MESSAGE = "ConfigProperties is immutable."


  override fun keySet(): Set<String> {
      return delegate.keys
  }

}

任何提示要解决这个问题?谢谢!

1 个答案:

答案 0 :(得分:0)

我认为您的问题始于MultivaluedMap<String,String>

abstract class ConfigProperties<K, V>(delegate: Map<K, V>?):
        MultivaluedMap<String, String> { ... }

暂时忽略String类型参数。 MultivaluedMap<K,V>是具有Map<K,List<V>>超级接口的接口。但是在您的代码中,您的delegate类型为Map<K,V>。您尝试通过返回与setKey不同的Map<K,List<V>>来覆盖delegate.keys超级接口的Map<K,List<V>>.keys成员(即,您要覆盖其成员)。< / p>

所以,你可以试试以下......

abstract class ConfigProperties<K, V>(delegate: Map<K, V>?):
        MultivaluedMap<K, V> {
    protected val delegate: Map<K, List<V>>
    init {
        if (delegate == null) {
            throw NullPointerException("Config properties delegate must not be null.")
        }
        this.delegate = delegate
    }
    abstract fun putCacheProperty(key: Parameter, value: Any)
    abstract fun getCacheProperty(key: Parameter): Any
    protected val UNSUPPORTED_MESSAGE = "ConfigProperties is immutable."
    override fun keySet(): Set<K> {
        return delegate.keys
    }
}

对于String类型参数,您的意思是使用K,V吗?无论你的意思是什么,你都需要保持一致。