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)
-
Hiding implementation details
-
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)
- Attributes are encapsulated by member methods and classes are encapsulated by packages
Implementation steps for encapsulation (data as an example)
-
Privatize attributes
-
Provides a common set method for determining and assigning values to attributes
-
Access modifier provided by Java and Scala
def setXxx(Parameter name: type) :Unit = {
// Add business logic for data validationProperty = Parameter name}Copy the code
- Provide a public
get
Method 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
Scala
In order to simplify code development, when declaring an attribute in a class, the corresponding attribute is automatically providedsetter/getter
Method (method nameAttribute name _&eq/ Attribute name
); If property access is declared asprivate
, corresponding tosetter/getter
The access to the method isprivate
; If the property declaration omits access rights (the compiler automatically assigns them to itpublic
Access permission), corresponding tosetter/getter
The access to the method isprivate
- If you just do a simple one on a property
Set and get
, you only need to declare the attribute; Access properties directly usedObject. Properties
Can (essentially call the compiler automatically generatedgetter
Methods); Set properties directly to useObject. Property = XXX
Can (essentially call the compiler automatically generatedsetter
Methods)