In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
In this issue, the editor will bring you an example analysis of the improper use of Java access control. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.
01 importance of access control
Shu Qi once said, "I'm going to put back the clothes I took off one by one!" Now, she has done it-through her own efforts, she has not only won the recognition of the audience, won a lot of awards, but also reaped happiness. Although, it is true, there are still some friends who "recall the extraordinary years of the past" have kept some rare photos of Shu Qi in the depths of the computer hard drive.
I cite this example mainly to prove that access control (or hiding the specific implementation) has something to do with "the initial implementation is not appropriate"-if the permissions of Shu Qi's original photos were not so public, then you can burn it privately now; or in a different way; but there is no if.
In your initial programming career, I don't know if you've ever created such "good" code as in listing 1-1.
Program listing 1-1:
Public class NiubiUtil {
Public static Date getNextDay () {
Long millis = 24 * 60 * 60 * 1000
Try {
Thread.sleep (millis)
} catch (InterruptedException e) {
E.printStackTrace ()
}
Return new Date (System.currentTimeMillis ())
}
}
Originally, you just wrote this code for fun, but who knew it was called by a colleague named Xiaoer Brother without consideration. This call does not matter, the second brother's code really waited alive all day-Mr. Wang, the product manager, one day, on a whim, wanted to test the second brother's code, and then clicked the button to see the next day, and the program really waited there quietly until the next day really came.
As a result, in order to appease the restless mood of the users, Mr. Wang dragged the second brother directly out to offer sacrifices to Heaven; you shivered in the corner, saying silently in your heart, "Brother, you go well, brother is really sorry."
A bloody tragedy! Just because of the improper use of Java access control-the private method, the result error is written as public-- on the importance of access control.
02 Why do you need access control
I've been on the road to Java programming for many years, and naturally I've written countless code that once looked so "good". As time goes by, when you look back on the code in the past, there is always a kind of "did this really come from me?" The question.
This question has two meanings. One is to feel that it is really well written. It is like when Li Bai, who was old, saw "Wang Lushan Falls" written when he was young: "the incense stove peak illuminated by the sun produces purple smoke. From a distance, the waterfall looks like a white exercise hanging in front of the mountain. It seems that the bright water of the Milky way fell in nine days, and the current poured down from a height of 3, 000 feet." -- Yo, I wrote well when I was young.
The other layer is really poorly written, such as program listing 1-1, the past can not bear to look back ah, can only be with the wind, all with the wind, all with the wind!
For these poorly written code, I want to modify it every time I see it, expecting it to be more readable, understandable, and maintainable, but there is also great pressure under a beautiful vision. because there are so many other places that quote the code to be modified and move the whole body.
In the process of change and immutability, I found that access control is really too important. In the process of writing code, we need to try to follow the principle of defining other methods as private except those that must public. The advantage of this is that you don't have to worry about refactoring private methods because they are not accessed outside the class.
03 what is the protected permission modifier for
We know that the public permission modifier makes all the methods and properties of the class visible; the private permission modifier makes all the methods and properties of the class visible only internally; what about the protected permission modifier?
If there is a guy named Wang er, he has three attributes, namely, private life, external image and protected property, as shown in listing 3-1 of the program.
Listing 3-1:
Public class Wanger {
/ / Private life
Private String life
/ / external image
Public String image
/ / protected property
Protected String money
}
Wang er gave birth to a child named Wang Xiaoer, just like listing 3-2.
Listing 3-2:
Public class Wangxiaoer extends Wanger {
@ Override
Public String toString () {
Return "inheritable property:" + money + "; the person created:" + image + "; the private life that cannot be obtained:"
}
}
Wang Xiaoer can inherit Wang er's property money (it is said that Wang er saved 100 for Wang Xiaoer in the first month of his birth, and the interest rate since then is 12% every month. Now the deposit is 3758.17. Do you know how old Wang Xiaoer is now? ), can also imitate the external image of the father image, but can not live according to the father's private life style.
Notice the difference between the three permission modifiers?
Protected can protect Wang er's property and inherit it smoothly to Wang Xiaoer, which is not accessible to outsiders; public is not secure, because not only Wang Xiaoer can inherit access, but also others can access it; privatized, not only can not be accessed by the outside world, Wang Xiaoer can not inherit either.
There are two important functions of access control: the first is to prevent the outside world from touching places that should not be touched (private-modified member variables or methods); the second is to allow class designers to change the internal (private-modified member variables or methods) without having to worry about causing interference to the outside world.
The above is an example of improper use of Java access control shared by Xiaobian. If you happen to have similar doubts, you might as well refer to the above analysis. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un