以下三个Scala声明之间有什么区别?我理解general distinction between val and var。
class A(x: T){ ... }
class B(val x: T){ ... }
class C(var x: T){ ... }
答案 0 :(得分:3)
A
和B
之间的差异(val
和var
创建访问者):
class A(a: Int) {}
// Doesn't compile (value a is not a member of)
// (new A(1)).a
class B(val b: Int) {}
(new B(1)).b //> res0: Int = 1