我试图在Scala中覆盖匿名函数的toString,如下所示:
scala> ()=>{def toString="yes"; 1}
res1: () => Int = <function0>
Run Code Online (Sandbox Code Playgroud)
哪个不起作用 - 我希望res1以某种方式"是".
这可能吗?
你不能做到这一点与匿名函数文本,则需要延长的Function特点.例如
val f = new (() => Int) {
override def toString = "yes"
def apply() = 1
}
Run Code Online (Sandbox Code Playgroud)
要么
val f = new Function0[Int] {
override def toString = "yes"
def apply() = 1
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
749 次 |
| 最近记录: |