JAVA_SE basics-28. encapsulation, java_se28 Encapsulation

Source: Internet
Author: User

JAVA_SE basics-28. encapsulation, java_se28 Encapsulation

Blog of Dark Horse programmer...


Three features of object-oriented architecture:
1. Encapsulation
2. Inheritance
3.


Today, we will first learn the largest feature, encapsulation.

Encapsulation: hides the attributes and implementation details of an object and only provides public access.


Benefits:
1. Isolate changes.
2. Easy to use.
3. improve reusability.
4. improve security.


Encapsulation principles:
1. Hide all content that does not need to be provided externally.
2. Hide all attributes and provide public methods to access them. For example, getXXX and setXXX.


Permission modifier: The permission modifier controls the visible range of variables.


Public: public. Public-modified member variables or methods can be directly accessed by anyone.


Private: private, private modified member variables or methods can only be directly accessed in this class.




Encapsulation steps:
1. Use private to modify the attributes to be encapsulated.
2. provides a public method to set or obtain the private member attributes.
Naming rules:
Set attribute name ();
Get attribute name ();


Q: Is the get or set method required for encapsulation?
Not necessarily, depending on your needs.


Standard: in actual development, all member attributes (member variables) of the object class must be encapsulated.


Entity class: An entity class is called an entity class used to describe a class of things.


Encapsulation. To avoid direct modification to the data domain, use the private modifier to declare the data domain as private. This is called data field encapsulation)

Objects outside the class that defines the private data domain cannot access a data domain. However, clients often need to access and modify data domains. To access private data domains, you can provide a get method to return the values of data domains. To update a data field, you can provide a set method to set a new value for the data.


Encapsulation of the vernacular explanation: original subject link: http://blog.csdn.net/chenssy/article/details/12757911

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.


If you have not yet understood it, let's take a look at the example below;

Class Person {publicString name; // name privateString sex; // gender publicint salary; // salary // defines a public method to set the sex attribute public void setSex (String s) {if (s. equals ("male") | s. equals ("female") {// Note: if the content of the two strings is consistent, do not use = for comparison. Use the equals method. Sex = s;} else {// The default is male sex = "male" ;}}// define a public method to obtain the sex attribute public String getSex () {return sex ;} // public void talk () {System. out. println (name + "have a good time");} class Demo5 {public static void main (String [] args) {Person = new Person (); m. name = "Zhang Jiayi"; m. setSex ("female"); m. salary = 9900; System. out. println ("name:" + m. name + "Gender:" + m. getSex () + "Salary:" + m. salary );}}

For example, the above Code is Weibo. Generally, girls do not like to fill in their real age, so they can set their weibo posts as private. This function is like a private keyword. The property of the age member is privatized and can only be viewed by yourself. Private attributes can only be accessed in its class. To allow external access to private attributes, you need to provide some public methods that use the public modifier, including getXxx () method and setXxx () method for setting the property value.





Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.