When we use Java, we often define some fields as private types to complete encapsulation, so that the outside world cannot access them.
If the outside world accesses or modifies the field, it can only be achieved through the getter and setter methods provided by the field.
There is no getter and setter in Scala.
Use value and value_= to replace getter and setter respectively.
Let's look at an example
class Counter{ private var privateValue = 0;//Private variables, which cannot be directly accessed by the outside world def value = privateValue;//Define a method, the method name is the name of the field we want, instead of the getter def value_= ( newValue : Int ){//The subject value_= is the method name value = newValue; } } object MyCounter{ def main(args :Array[String]){ val myCounter = new Counter; println(myCounter.value);//Call value method to access value, equivalent to getter myCounter.value = 3;//Set a new value for value, equivalent to setter } }