作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
下面是 Scala 隐式类的示例程序:
object Run {
implicit class IntTimes(x: Int) {
def times [A](f: =>A): Unit = {
def loop(current: Int): Unit =
if(current > 0){
f
loop(current - 1)
}
loop(x)
}
}
}
4 times println("hello")
”如下,但我不明白“
4 times println("hello")
”是什么意思?
object Demo {
def main(args: Array[String]) {
4 times println("hello")
}
}
最佳答案
4 times println("hello")
大致翻译成:
val c = new IntTimes(4)
c.times(println("hello"))
Int
作为其唯一参数,使用方法
times
, 做
4.times
使用 4 作为参数隐式实例化类,然后调用
times
在上面。
关于scala - Scala 中的隐式类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40878893/
我是一名优秀的程序员,十分优秀!