Understand the encapsulation of Java's three major features and java's three major features

Source: Internet
Author: User

Understand the encapsulation of Java's three major features and java's three major features
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.

Public class Husband {/** encapsulation of attributes * the name, gender, age, and wife of a person are all private attributes of the person */private String name; private String sex; private int age; private Wife wife;/** setter (), getter () is the interface for external development of this object */public String getName () {return name ;} public void setName (String name) {this. name = name;} public String getSex () {return sex;} public void setSex (String sex) {this. sex = sex;} public int getAge () {return age;} public void setAge (int age) {this. age = age;} public void setWife (Wife wife) {this. wife = wife ;}}
public class Wife {    private String name;    private int age;    private String sex;    private Husband husband;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getSex() {        return sex;    }    public void setSex(String sex) {        this.sex = sex;    }    public void setAge(int age) {        this.age = age;    }    public void setHusband(Husband husband) {        this.husband = husband;    }    public Husband getHusband() {        return 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:

public class Husband {    public String name ;    public String sex ;    public int age ;    public Wife wife;}

We should use it like this:

Husband husband = new Husband (); husband. age = 30; husband. name = "zhangsan"; husband. sex = "male"; // seems redundant

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.

Public class Husband {/** encapsulation of attributes * the name, gender, age, and wife of a person are all private attributes of the person */private String name; private String sex; private String age;/* change to String type */private Wife wife; public String getAge () {return age;} public void setAge (int age) {// convert this. age = String. valueOf (age);}/** the setter and getter attributes of other attributes are omitted **/}

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

Here we can see that encapsulation can make 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 = new Husband();        husband.age = 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:

Public class Husband {/** encapsulation of attributes * the name, gender, age, and wife of a person are all private attributes of the person */private String name; private String sex; private int age;/* change to String type */private Wife wife; public int getAge () {return age;} public void setAge (int age) {if (age> 120) {System. out. println ("ERROR: error age input .... "); // The prompt message" Response response "} else {this. age = age ;}}/** the setter and getter attributes of other attributes are omitted **/}

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.

Public String getSexName () {if ("0 ". equals (sex) {sexName = "female";} else if ("1 ". equals (sex) {sexName = "male";} else {sexName = "female ??? ";} Return sexName ;}

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

Public String getCzHTML () {if ("1 ". equals (zt) {czHTML = "<a href = 'javascript: void (0) 'onclick = 'qy (" + id + ") '> enable </a> ";} else {czHTML =" <a href = 'javascript: void (0) 'onclick = 'jy ("+ id + ") '> disable </a> ";} return czHTML ;}

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!

Notes for learning Java !!!
If you have any questions or want to obtain learning resources during the learning process, join the Java learning exchange group: 159610322 let's learn Java together!

Related Article

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.