Packaging: The class of variable privatization, does not allow direct access to external programs, use get \ set method for viewing modify operation.
Benefits: hides the implementation details of a class can only be accessed through a specific method of operating restrictions unreasonable, safer
Reflects the relative independence of things, to avoid the influence of external operations this object,
Loosely coupled: to minimize the correlation between the objects, in order to reduce complexity and dependencies between them
step:
The main steps: a class attribute privatization added getters () of private property / the setter () method of setting the necessary read restriction
Visibility 1, modified properties
Use private variables modified so that it can only be used in this class
2, set the attribute getter () / setter () method
Check required
3, set the attribute storage limit
Attribute value validity check process to be limiting in the setter
For example: only two kinds of sex, age can not live nor too big
public String getGender() { return gender; } public void setGender(String gender) { if(gender.equals("男")|| gender.equals("女")) { this.gender = gender; }else { System.out.println("冷静,性别只有男女!!!"); } }