Why set and get method action properties in a Java class

Source: Internet
Author: User

It is recommended in the Java program specification that you privatize the properties in the class, defined as private variables, and manipulate the properties by setting the set and get functions. Some people have this question, why not directly set the property to public, the subsequent invocation of the property directly through the object access is not more convenient?

Of course there's a reason for this in Java:

1Separating data from behavior is the object-oriented concept of java.
object is the attribute + behavior, encapsulation is the object's private properties are encapsulated, only through their own public behavior to change (get) The private property information inside the object, and those who use the method is the interface for other objects, only through the interface to change (get) The private properties of the object

2  security  
    Imagine that you have a person class that represents a man who has a char type of sex field that represents gender, theoretically, sex only accepts two values, ' m ' and ' f ', but if you set the sex field to public, it's hard to limit your users to assigning only ' M ' or ' f ' values to it. By setting sex to private and then using Setsex () to set the gender, you can control this behavior completely. And you can control only get cannot set, or vice versa, but if it is public it will not. There's a little bit of a property. We may not want other users to write about our properties, which can be done directly without writing the Setx method. This is the read-only attribute.

3   coding normative  
     we are in the design class, or as a COM component often tell the truth coder, you only need to call my XX interface method can be, internal how I realized, you do not have to tube, If you are declaring a property as public for a specific time period for a particular environment, and if the external object is accessible or not, without the idea of a face object, the variables in the structure that I write in the C language program are freely accessible because there is no pulbic,private. Now we are object-oriented programming, we need to do the article at the interface, but the protection of the object's private property, that is, security, only through the externally exposed interface method to access in the object-oriented theory is considered safe.

4   stability

5    extensibility

6   for ease of maintenance

Why set and get method action properties in the Java class

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.