Why does java need encapsulation?

First look at the classic manifestation of [encapsulation] in java:
objects are not allowed to use [dot operator] to directly [access] the value of instance variables;
eg:
Insert picture description here
like the above, you can directly use [dot operator] to change dog1 The value of the age variable of the instance is extremely unacceptable in the Java world, because it violates the iron law of encapsulation! ! !
As a result, the data is unreasonably exposed. [The exposure here refers to-you can use the [dot operator] to directly [access] the value of the instance variable]

Therefore, the correct way of writing is:
Insert picture description here
Therefore, you can summarize the basic principles of java encapsulation:
mark the instance variables of the class as private with private,
and provide public Getter and Setter methods to store the corresponding instance variables.

The effect of this principle is: it
cuts off the possibility of other classes directly storing object instance variables through the [dot operator], and forces all want to access the value of an object instance variable through Setter. Variable access;
then, the process of accessing instance variables has changed from direct [dot storage] to first calling Setter to access the variable value; [additional Setter step]
That is to say: we can use Setter The assignment judgement of the legality is done, and the assignment is allowed after the judgement is passed, thereby ensuring the safety of the instance variable value (ie: ensuring that the value of the instance variable is within a legal range)
eg:
Insert picture description here
As shown above: we are in Setter Some [legitimate assignment judgments] have been added to ensure the legitimacy of the age value;

When you see this, you might think:
Isn't it the same as if I write like the following to ensure that age is worthy of legitimacy?
Insert picture description here
Yes, writing like the above can guarantee the legitimacy of age, but it violates the encapsulation of java and is not conducive to the later expansion of the code.
Explanation:
Assume a scenario:
if the value of age does not need to be assigned and verified at the beginning, the direct use of [dot operator for instance variable access] is written as: dog1.age = 10,
this writing appears in the code 1000 times.

Later, the business scenario changed, requiring the [Assignment Legality Judgment] to be performed on the value of age. Is it necessary to add 1000 if judgments based on the above wording?

OMG, this kind of [code design] is simply unacceptable! ! !

What if you use the encapsulation method from the beginning?
Is it enough to add an if judgment to the Setter method of the instance variable of age?

From this we can see the superiority of the encapsulation method: a place is reserved for judging the validity of the assignment of instance variables

In addition, in actual coding, our most common Setter method actually does not have any code for [Assignment Legality Judgment], like the following:
Insert picture description here
So, the purpose of encapsulation is: I don’t need to write the code for [Assignment Legality Judgment]. , But if needed, I can immediately add it in the most [elegant] way! ! !

Therefore, [the simplicity brought by the direct use of the dot operator to access variables] is not worth mentioning compared to the [reservation of assignment legality judgment] brought by the above encapsulation! ! !

Summary:
[Encapsulation] combined with [Assignment legality judgment] can ensure that the instance variables of the object are not assigned to any weird values , thus ensuring the data security when the program is running! ! !

Guess you like

Origin blog.csdn.net/weixin_44334964/article/details/113920017