Java improvements ----- understand the encapsulation of the three main features of java

Source: Internet
Author: User

It has been almost three years since I joined java in my sophomore year. From the most basic HTML and CSS to the final SSH, I walked out step by step, in which I was happy, lost, and lonely. Although I was half a monk, I had completed my studies through my own efforts ". I attended a training institution, but did not like the training method very much. So I gave up my choice of self-study (poor, I paid 6000 yuan). Although I had a lot of difficulties on the way to self-study, but I am more happy. I only know what I pay and what I get. Huang Tian is an easy-to-use person. He was stupid. He finally learned java by himself in the first semester of his senior year (he took a detour and spent half a year). He also got a good job with him. I am very grateful!

Too many gossip! As I entered the topic, LZ has just finished reading the design model and has a lot of feelings. In addition, I deeply feel that the java foundation is not solid enough in the course of work, for example, I am confused about three major features, I am not familiar with IO, I am not familiar with garbage collection, I am confused about the polymorphism, I am not aware of reflection, and even the most basic features! So I am so worried that I must make up for the java Foundation! Starting from Lesson 1 --- encapsulation !!!!!!

Three features --- Encapsulation

Encapsulation literally refers to packaging. The professional point is Information Hiding. It refers to encapsulating data and data-based operations by using abstract data types, make it an inseparable and independent entity. The data is protected inside the abstract data type, and the internal details are hidden as much as possible, so that only some external interfaces are kept in touch with the external. Other objects in the system can communicate and interact with the encapsulated object only through authorized operations wrapped outside the data. That is to say, the user does not need to know the internal details of the object (of course, they do not know), but can access the object through the interface provided by the object.

For encapsulation, an object encapsulates its own attributes and methods, so it can complete its operations without relying on other objects.

Three Advantages of encapsulation:

1. Good encapsulation can reduce coupling.

2. The internal structure of the class can be freely modified.

3. More precise member control.

4. Hide information and implement details.

First, let's look at two classes: Husband. java and Wife. java.

           .name = .sex =   setAge(.age = .wife =
   .name = .sex =  setAge(.age = .husband =

From the above two instances, we can see that the wife reference in Husband does not have getter (), and the wife age does not have the getter () method. As for the reason, I want everyone to understand it. For a man, I want to know her age without a woman.

Therefore, encapsulation privatize the attributes of an object and provides some methods for attributes that can be accessed by the outside world. If we do not want to be accessed by the outside world, we do not need to provide methods for the outside world. However, if a class is not provided for external access, it makes no sense. For example, we think of a house as an object. The beautiful decoration in it, such as SOFA, TV series, air conditioners, and tea tables, are all private properties of the House. However, if we do not have these walls, will it be visible to others? No privacy! There is the wall that is blocked. We can have our own privacy and we can change the decoration at will without affecting others. But if there are no doors and windows, what is the significance of a wrapped black box? So other people can see the scenery through the doors and windows. Therefore, doors and windows are the interface for external access from House objects.

We cannot really appreciate the advantages of encapsulation. Now we analyze the benefits of encapsulation from the perspective of programs. If we do not use encapsulation, then the object will not have setter () and getter (), then the husb and class should be written as follows:

  

We should use it like this:

Husband husband = 30 = "Zhang San" = "male ";

But what if we need to modify Husband that day, for example, to change the age to the String type? It's okay that you only use this class in one place. If you have dozens or even hundreds of such places, do you want to change to a crash. If encapsulation is used, we do not need to make any modifications. We only need to slightly change the setAge () method of the Husband class.

      String age ;          setAge(        .age =

The reference (husband. setAge (22) remains unchanged in other places.

Here we can see that,Encapsulation makes it easy for us to modify the internal implementation of the class without modifying the customer code that uses the class..

We are looking at this advantage: we can more accurately control member variables.

Or the husb and, in general, we are not prone to errors when referencing this object, but sometimes you get confused and write it like this:

Husband husband = = 300;

Maybe you wrote it carelessly. You found it okay. If you didn't find it, it would be too much trouble. Maybe someone has ever seen a 300-year-old monster!

However, we can avoid this problem by using encapsulation. We have some control over the access entry (setter) of age, for example:

       age ;           setAge((age > 120"ERROR:error age input....");            }.age =

The above is the control of the setter method. In fact, through the use of encapsulation, we can also make good control over the exit of the object. For example, gender is usually stored in the database in 1 or 0 mode, but we cannot display 1 or 0 at the front end. Here we only need to go to getter () method.

("0" = "female" ("1" = "male" = "female ??? "

You only need to use sexName to display the correct gender. Similarly, it can be used to perform different operations on different states.

("1" = "<a href = 'javascript: void (0) 'onclick = 'qy (" + id + ") '> enabled </a> "=" <a href = 'javascript: void (0)' onclick = 'jy ("+ id + ") '> disable </a>"

The monks are not easy to learn. They can only comprehend so much. If there are any mistakes in this article, I hope to correct them. I am very grateful to you!

Tucao

After three days in the house, I went out for a walk today and met the best girl on the bus! I sat down, got on the bus, stood beside me, and started to be okay. After a while, she smiled at me and smiled, but you couldn't always smile at others! I'm not happy with the smile (my face is thin, easy to shy ??). Didn't I wash my face? Looking in the mirror is pretty clean! Isn't my clothes or trousers **? It seems that my clothes and trousers are pretty good! Is it me handsome, interesting to me? (It seems impossible for a programmer to have a piece of silk )! I can't stand it anymore. Why can't I hide it? Get off! When I got off the bus, that pretty girl quickly took my seat! At that time, brother had a word: Rely !!!!!!!!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.