HashMap如何在Kotlin中实现MutableMap接口?

时间:2017-03-04 16:08:54

标签: java kotlin

(注意:下面有一个Kotlin Koans的潜在扰流板。)

给定一个更高阶的函数,它接受这样的函数文字:

  markers: marker[] = [
  {
      lat: 51.673858,
      lng: 7.815982,
      label: 'A',
      profileImage: "http://www.gravatar.com/avatar/d735414fa8687e8874783702f6c96fa6?s=90&d=identicon&r=PG"
  },
  {
      lat: 51.373858,
      lng: 7.215982,
      label: 'B',
      profileImage: "http://placekitten.com/90/90"
  }
]

java.util.HashMap如何满足fun <K, V> buildMap(build: MutableMap<K, V>.() -> Unit): Map<K, V> { // How does java.util.HashMap satisfy the MutableMap interface? // Does Kotlin support ducktyping? val map = HashMap<K, V>() map.build() return map } 针对的MutableMap接口? Kotlin是否支持某种类型的sptyping,或者这是Kotlin语言中的特殊情况,仅适用于属于JDK的某些类?

我看了the Kotlin documentation on interfaces并搜索了一下,但找不到任何似乎可以解释这一点的内容。

1 个答案:

答案 0 :(得分:3)

Kotlin in Action一书中提到了关于ArrayListHashMap的以下内容:

  

Kotlin认为他们好像分别继承了Kotlin的MutableListMutableSet接口。

所以基本上是的,就像你提出的那样,这些集合的特殊情况是在Kotlin编译器中以某种方式实现的。不幸的是,这是他们提供的所有细节,所以如果你想了解更多,你可能不得不深入研究编译器的来源。

我认为主要的一点是,这不是您可以自己使用的语言功能。