encapsulation

From the perspective of data encapsulation, abstract data is encapsulated with operations on data. Data is protected internally, and other parts of the program can perform operations on data only through authorized operations (member methods)

From the perspective of modules: encapsulation is to encapsulate the logic that needs to be reused into common modules to improve development efficiency and reduce repetitive coding

Understanding and benefits of encapsulation (data as an example)
  1. Hiding implementation details

  2. It can not only verify the data, but also ensure that the data is safe and reasonable

How to embody encapsulation (data as an example)
  1. Attributes are encapsulated by member methods and classes are encapsulated by packages
Implementation steps for encapsulation (data as an example)
  1. Privatize attributes

  2. Provides a common set method for determining and assigning values to attributes

  3. Access modifier provided by Java and Scala

def  setXxx(Parameter name: type) :Unit = {
		// Add business logic for data validationProperty = Parameter name}Copy the code
  1. Provide a publicgetMethod to get the value of an attribute
def getXxx() [: return type] = {returnAttribute}Copy the code
Scala encapsulation case demonstration

Create an employee class, privatize the necessary attributes, and limit the age to [0,120]

object StuffDemo {
  def main(args: Array[String) :Unit = {
    val stuff = new Stuff(sex = 'M')
    println(stuff) / / Stuff (2000.0, 18, M)

    stuff.setAge(24)
    println(stuff) / / Stuff (2000.0, 24, M)

    stuff.setAge(190)
    println(stuff) / / Stuff (2000.0, 24, M)}}/** * You can define default values for attributes in the main constructor, or even set access permissions for attributes, But you lose control of the properties (the workaround is that you can validate the set properties before calling the constructor) * * @param salary * @param age * @param sex */
class Stuff(private var salary: Double = 2000, val sex: Char) {
  private var age: Int = 18

  def setAge(age: Int) :Unit = {
    if (age >= 0 && age <= 120) {
      this.age = age
    }
  }

  override def toString = s"Stuff($salary.$age.$sex)"
}
Copy the code
details
  1. ScalaIn order to simplify code development, when declaring an attribute in a class, the corresponding attribute is automatically providedsetter/getterMethod (method nameAttribute name _&eq/ Attribute name); If property access is declared asprivate, corresponding tosetter/getterThe access to the method isprivate; If the property declaration omits access rights (the compiler automatically assigns them to itpublicAccess permission), corresponding tosetter/getterThe access to the method isprivate
  2. If you just do a simple one on a propertySet and get, you only need to declare the attribute; Access properties directly usedObject. PropertiesCan (essentially call the compiler automatically generatedgetterMethods); Set properties directly to useObject. Property = XXXCan (essentially call the compiler automatically generatedsetterMethods)