[Javase Study Notes]-6.7 Encapsulation
In this section, we will learn the first feature of the object-oriented model, encapsulation)
Encapsulation: hides the occurrence and implementation details of an object and only provides public access.
So what is the Implementation Details of hiding objects? Let's give an example.
For example, Let's define a person's class. Simply put, people's attributes include age and behavior.
Let's look at the following code:
Class Person // defines a Person's class {int age; void speak () {System. out. println ("age =" + age) ;}} class PersonDemo // define the test class {public static void main (String [] args) {Person p = new Person (); p. age =-20; // The code is still running normally, which causes security risks. speak ();}}
Result:
We can see that there are security risks. When we assign a negative value to the person's age attribute, the program runs normally.
Now modify the Person class.
class Person { private int age; void speak() { System.out.println("age = "+age); } }
Result:
An error is reported because the age is private and cannot be accessed in the PersonDemo class.
To further solve the problem, we need to modify the two classes:
Class Person {private int age; // set this member attribute to private public void setAge (int a) {if (a> 0 & a <130) {age =; speak ();} elseSystem. out. println ("wrong data");} void speak () {System. out. println ("age =" + age) ;}} class PersonDemo {public static void main (String [] args) {Person p = new Person (); p. setAge (-20); // call the method to set the age, but we do not know the internal implementation of this method }}Result:
We can see that it is valid only when age> 0 & age <130.
From this process, we can see that when we want to operate the age attribute in Person, it is implemented through the setAge method, but how is the setAge method implemented? External classes cannot be known, this is the property and implementation details of the hidden object, that is, encapsulation.
In fact, for external access, we generally have standard get and set methods. Let's take a look at two common and standard methods:
Class Person {private int age; // set this member attribute to private public void setAge (int a) // set age {age = a;} public int getAge () // get age {return age ;}}
Next, let's talk about the advantages and principles of encapsulation:
Advantages:
1. Improve code security;
2. controllability of internal attributes;
3. Improve code reusability;
4. Isolated changes;
Principles:
1. Hide all content that does not need to be provided externally;
2. Hide attributes and provide public methods for external access (of course not all methods must be public methods ).
Finally, let's take a look at a keyword: private.
Private keywords: Private, which is a permission modifier used to modify members. Private content is only valid in this class.
Its common usage is to privatize member variables, provide the corresponding set and get methods for external access, and improve the security of data access.
Note: Private is only an embodiment of encapsulation.
For java, the smallest encapsulation body is a function. class is also the encapsulation body. the framework itself is also an encapsulation body. these are all common, that is, we can use them directly without knowing the implementation details.