Java learns thirteen from scratch (encapsulation), and java encapsulates from scratch
1. What is encapsulation and why?
For object-oriented objects, encapsulation is to package methods and attributes into a program unit, and this unit is implemented in the form of classes.
To put it simply, blocking is to privatize attributes and provide public methods to access private attributes.
Encapsulation:
- Encapsulation reflection and the relative independence of things. The function of encapsulation in programming is to prevent other parts of the object from arbitrarily changing the attributes of the object, thus effectively avoiding the impact of external error operations on the object.
- Improves loose coupling and code reusability
Loose coupling:Think of the object as a battery. This battery can be used not only in the camera, but also in the remote control, hair dryer and shaving hair. We say the loose coupling of the battery is very good, the premise for achieving such a good loose coupling is that the object has a good encapsulation.
Ii. encapsulation implementation
Package com. pb. demo3; public class Person {private String name; // name private String sex; // gender private int age; // age // set the getter and setter methods to public String getName () {return name;} public void setName (String name) {this. name = name;} public String getSex () {return sex;} public void setSex (String sex) {if (sex. equals ("male") | sex. equals ("female") {// sets the Gender limit. If the value is invalid, an error is returned. sex = sex;} else {System. out. println ("invalid gender, Gender: male or female") ;}} public int getAge () {return age;} public void setAge (int age) {if (age> 0 & age <= 150) {// set the age limit this. age = age;} else {System. out. println ("age must be between 1-years old") ;}} public Person () {}public Person (String name, String sex, int age) {this. name = name; this. sex = sex; this. age = age;} public void say () {System. out. println ("self-introduction:"); System. out. println ("name:" + this. name); System. out. println ("Gender:" + this. sex); System. out. println ("Age:" + this. age );}}
Test class:
Package com. pb. demo3; public class PersonTest {public static void main (String [] args) {Person person Person = new person (); Person. setName ("HAN Bing"); // enter an invalid person value. setSex ("neutral"); // enter an invalid person value. setAge (200); // call a common method to output person. say ();}}
Result: an error is displayed.
The gender is invalid. The gender can only be male or female.
The age must be between 1 and years old.
Self-Introduction:
Name: Han Bing
Gender: null
Age: 0
The result shows that the invalid value is the initial value of the data type. String is null, and int is 0.
You can also initialize the value in the constructor. In this way, when the value is set to invalid, the initialized value will be used.
Public Person () {this. name = "anonymous"; this. sex = "male"; this. age = 22 ;}
When an incorrect value is passed in
Package com. pb. demo3; public class PersonTest {public static void main (String [] args) {Person person Person = new person (); Person. setName ("HAN Bing"); // enter an invalid person value. setSex ("neutral"); // enter an invalid person value. setAge (200); // call a common method to output person. say ();}}
Result:
The gender is invalid. The gender can only be male or female.
The age must be between 1 and years old.
Self-Introduction:
Name: Han Bing
Gender: male
Age: 22