Encyclopedia: Encapsulation: Refers to the properties of hidden objects and implementation details, only provide public access to external, such as computer chassis, notebook, etc.
Benefits:
Isolate the changes;
easy to use;
Improve reusability;
Improved security (prevent arbitrary modification)
One package, can be called indefinitely, and the attribute can be qualified
Keyword private: The embodiment of encapsulation in code
(1) private meaning, permission modifier
(2) To modify member variables and member functions
(3) Members with private decoration are valid only in this class, and members of this class can call the private property directly
(4) Private is a manifestation of encapsulation
The advantage of encapsulating a basic data type as an object is that more functionality can be defined in the object to manipulate the data.
Personal: Set method has parameters, no return value, public void setage (int age) No return
The Get method has no parameters, there is a return value of public int getage () return of age;
1 //For example, the following code:2 3 Private intAge ;4 Public voidSetage (intAge ) {5 6 if(age>150| | Age<0){7 return;//when age is assigned to a value that does not meet the requirements, returns an empty8 }9 This. age=age;//when it meets the requirementsTen } One Public intGetage () { A returnAge ; - -}
Encapsulation method or Property