一个例子让你明白Scala中的call by name和call by value
先看一个例子
def get(x:Int):Int={
println(s"$x")
x
}
def getByValue(y:Int):Int={
x+x
}
def getByName(y: =>Int):Int={
x+x
}
getByValue(get(1))
getByName(get(2))
(1)get方法
该方法传入x,先打印x,然后将x作为返回值。
(2)getByValue和getByName
这两个方法定义非常相似,参数写法不同,getByName参...
原创
2020-05-30 18:09:07 ·
833 阅读 ·
0 评论