dha*_*0us 0 scala immutability
class Time(var h: Int, val m: Int) {
def before(val other: Time) = { //compile error due to keyword val
(this.h < other.h) || (this.m < other.m)
}
}
Run Code Online (Sandbox Code Playgroud)
我要怎样的说法其它的方法之前为VAR/VAL?如果我在其他之前删除val,它会成功编译.
您无法修改引用,other因为它是函数的参数.
def before(val other: Time) = ...
Run Code Online (Sandbox Code Playgroud)
相当于(如果它是合法的)
def before(other: Time) = ...
Run Code Online (Sandbox Code Playgroud)
如果你想要一个var,只需在函数内创建它:
def before(other: Time) = {
var otherVar = other
...
}
Run Code Online (Sandbox Code Playgroud)