私はScalaを学んでいますが、私はそれを理解するのが難しいです。私はチュートリアルの1つでいくつかの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
を、特別なbenifitまたはプライベートとして理解するだけの慣習があります。何が
_=
でdef age_=(newAge:Int) = _age = newAage
これが何をしているか。
http://dustinmartin.net/getters-and-setters-in-scala/ –