我喜欢下面写的地图,我的实现为mutable.HashMap。
class SampleMap() extends mutable.HashMap[String, (Any, BigInt)]
Run Code Online (Sandbox Code Playgroud)
映射的+ =方法和其他方法都是从超类使用的,而不是其他方法。下面的作品完美。
override def +=(kv: (String, (Any, BigInt))): this.type = {/*compiled code*/}
Run Code Online (Sandbox Code Playgroud)
现在,我想编写我的++ =方法的自定义实现,该方法是Map从可增长类继承的。当我编写重写的方法++ =时,编译器没有抱怨要重写的内容。
override def ++=(currentMap: MergeMap): this.type = {
Run Code Online (Sandbox Code Playgroud)
如何为我的自定义Map编写此方法的自定义实现。
尝试覆盖scala.collection.generic.Growable.++=方法:
class SampleMap() extends mutable.HashMap[String, (Any, BigInt)] {
override def ++=(xs: TraversableOnce[(String, (Any, BigInt))]): this.type = {
this
}
}
Run Code Online (Sandbox Code Playgroud)
然后效果很好:
def main(args: Array[String]): Unit = {
new SampleMap() ++= mutable.HashMap.empty[String, (Any, BigInt)]
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
53 次 |
| 最近记录: |