Define getter and setter in scala
the naming convention
Let’s see an example.
class GetterAndSetter {
private var _i = 3
// define getter
def i: Int = _i
// special naming, define setter
def i_=(x: Int): Unit = {
_i = x
}
}
object GetterAndSetter {
def main(args: Array[String]): Unit = {
val gs = new GetterAndSetter
// set i directly by dot method
gs.i = 1
println(gs.i)
}
}