Scala 程序员应该知道这种写作:
class Person{
var id = 0
}
var p = new Person
p.id
p.id = 2
等于
class Person{
private var _id = 0
def id = _id
def id_=(i: Int) = _id = i
}
val p = new Person
p.id // be equal to invoke id method of class Person
p.id = 2 // be equal to p.id_=(2)
有效。但是如果你注释getter方法def id = _id
,p.id = 2
会导致编译错误,说
error: value key is not a member of Person
谁能解释为什么?