0
我在网上当然看到这个class
定义,我走:更新一个字段声明为VAL
class Img(val width: Int, val height: Int, private val data: Array[Int]) {
def this(w: Int, h: Int) = this(w, h, new Array(w * h))
def apply(x: Int, y: Int): Int = data(y * width + x)
def update(x: Int, y: Int, c: Int): Unit = data(y * width + x) = c
}
我很困惑。我们如何更新data
这样的data(y * width + x) = c
即使它被宣布为val
?
这是一个更广泛使用的模式?
它有点像Java中的final int [10]数据。你不能改变参考,但如果它是可变的,在这种情况下它可以是内容。 –