Java-based package

Source: Internet
Author: User

Three characteristics of the---package

Encapsulation literally is the meaning of packaging, the professional point is information hiding, refers to the use of abstract data types to encapsulate data and data-based operations together to form an indivisible independent entity, the data is protected in the abstract data type inside, as far as possible to hide the internal details, Only some external interfaces are retained to make them contact external. Other objects of the system can communicate and interact with this encapsulated object only through authorized operations that are wrapped outside the data. This means that the user does not need to know the details inside the object (and of course it is unknown), but can access the object through the interface provided by the object externally.

For encapsulation, an object encapsulates its own properties and methods, so it does not need to rely on other objects to do its own work.

There are three major benefits to using encapsulation:

1, good package can reduce the coupling.

2, the structure within the class can be freely modified.

3, the member can be more precise control.

4, hide information, realize the details.

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

public class Husband {        /     * * Property Encapsulation     * A person's name, gender, age, wife are the private attributes of this person */    private String name;    Private String sex;    private int age;    Private Wife Wife;        /     * Setter (), getter () is the interface developed by the 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.) {        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.) {        this.age = age;    }    public void Sethusband (Husband Husband) {        this.husband = Husband;    }    Public Husband Gethusband () {        return Husband;    }    }

From the above two examples we can see that husband inside the wife reference is no getter (), while the wife of age is no getter () method. As for the reason I think everybody understands, the man is deep house hides the wife, no that woman wants others to know her age.

So encapsulation is to privatize the properties of an object, while providing some of the properties that can be accessed by the outside world, and if we don't want to be outside the method, we don't have to provide a way for outside access. But if a class does not provide access to the outside world, then this class is meaningless. For example, we think of a house as an object, the beautiful decorations in it, such as sofas, TV dramas, air conditioners, tea table and so on are all private properties of the house, but if we do not have those walls, is it someone else will be sweeping? Not a bit of privacy! Is that there is a wall of shelter, we can have their own privacy and we are free to change the contents of the inside without affecting the other. But what does it mean if there are no doors and windows, a wrapped black box? So through the doors and windows others can see the scenery inside. So the doors and windows are the object of the house to the outside access interface.

Through this we can not really understand the benefits of encapsulation. Now we analyze the benefits of encapsulation from the perspective of the program. If we do not use encapsulation, then the object does not have a setter () and Getter (), then the husband class should write:

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

We should use it this way:

Husband Husband = new Husband ();        Husband.age = +;        Husband.name = "Zhang San";        Husband.sex = "male";    Seems a little superfluous.

But that day if we need to modify the husband, for example, to change the age to a string type? You have only one place to use this class OK, if you have dozens of or even hundreds of such places, you are not going to change to collapse. If we use encapsulation, we can simply change the Setage () method of the next husband class without having to make any changes.

public class Husband {        /     * * Property Encapsulation     * A person's name, gender, age, wife are the private attributes of this 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.) {        //convert        This.age = string.valueof (age);    }        /** omitting other attributes of setter, getter **/    }

Other places still like that reference (Husband.setage (22)) remains the same.

As we can see here, encapsulation does make it easy for us to modify the internal implementation of the class without modifying the client code that used the class .

We're looking at the benefit: more precise control over member variables is possible.

Or the husband, in general we are not prone to error when quoting this object, but sometimes you are confused and written like this:

Husband Husband = new Husband ();        Husband.age = 300;

Perhaps you are because of careless written, you found that fortunately, if not found that the trouble, approaching who has seen the 300-year-old monster Ah!

But with encapsulation we can avoid this problem, we do some control (setter) on the access entry of age, such as:

public class Husband {        /     * * Property Encapsulation     * A person's name, gender, age, wife are the private attributes of this 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.) {        if (age > +) {            System.out.println ("Error:error Age input ...");    Prompt for wrong information        }else{            this.age = age;        }            }        /** omitting other attributes of setter, getter **/    }

The above is the setter method of control, in fact, through the use of encapsulation we can also be able to control the export of objects. For example gender we are generally in the database is 1, 0 ways to store, but at the front desk we can not show 1, 0, here we just need to do some conversion in the Getter () method.

Public String Getsexname () {        if ("0". Equals (Sex)) {            sexname = "female";        }        else if ("1". Equals (Sex)) {            sexname = "male";        }        else{            sexname = "Shemale???";        }        return sexname;    }

In use we only need to use sexname to achieve the correct gender display. The same can be used to make different actions for 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;    }


Transferred from: http://www.cnblogs.com/chenssy/p/3351835.html

Java-based package

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.