我正在学习Scala,但是我很难理解它。我在教程中得到了一些Scala代码,但是有几个问题我无法理解。
以下是代码:
class Person(val id:Int,
var firstName:String,
var lastName:String,
private var _age:Int) {
// these methods are identical to what would be created by var in the constructor
def age = _age
def age_=(newAge:Int) = _age = newAage
}
val me = new Person(45,"Dave","Copeland",35)
me.firstName = "David"
me.age = 36
我不明白:
为什么要用
_age
,而不是age
,这样做有什么特殊的好处还是只是一种理解为私有的惯例。def age_=(newAge:Int) = _age = newAage
中的_=
是什么意思,这个语句在做什么。