[Object Oriented Design Basics] access methods + private class members and Methods

Source: Internet
Author: User

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

I didn't want to write this because it was too basic. However, for the completeness of the study notes, I 'd like to write a few simple notes for myself.
1. the access method is also called getter and setter. It provides external interfaces for Private Members in the class. This design makes it necessary to modify the setter and getter at the end of program modification, instead of modifying all the things related to this member in the project, these setter and getter are not only used for objects outside the class, but are also used as much as possible during internal use, to improve the program's modifyability. Private class members and methods are important methods for implementing object-oriented encapsulation features. The related control letter permission levels are included in a previous article () there is a table and related descriptions for reference. The advantages of encapsulation are:
2. example: An interesting example is the husband and wife encapsulation example. You can see that the man class does not provide the getwife method, this means that a man does not want his wife to be accessed by the outside world. Encapsulate the attributes of an object and provide some methods for attributes that can be accessed by the outside world. For example, the name attribute, man, and woman classes all have corresponding get and set methods, the outside world can access and modify through these methods. At the same time, we do not provide methods for attributes that do not want to be accessed by the outside world. For example, there is no get method for the wife attribute of man, the outside world cannot be taken to the wife attribute of the man class.
Public class man
{
// Encapsulation of attributes. A person's name, age, and wife are all private attributes of this object (person ).
Private string name;
Private int age;
Private woman wife;
// Encapsulate the method provided by the modifier to the outside world. You can set the wife, name, and age to obtain the man's name and age.
Public void setwife (woman wife)
{
This. Wife = wife;
}
Public String getname ()
{
Return name;
}
Public void setname (string name)
{
This. Name = Name;
}
Public int getage ()
{
Return age;
}
Public void setage (INT age)
{
This. Age = age;
}
}
Public class woman
{
// Attribute Encapsulation
Private string name;
Private int age;
Private man husband;
// Method Encapsulation
Public String getname ()
{
Return name;
}
Public void setname (string name)
{
This. Name = Name;
}
Public int getage ()
{
Return age;
}
Public void setage (INT age)
{
This. Age = age;
}
Public man gethusband ()
{
Return husband;
}
Public void sethusband (man husband)
{
This. Husband = husband;
}
}
3. Advantages:
1) internal implementation can be changed, except for the method of this class, it will not affect other code.
For example, if you change the name-based access method to firstname + ''+ lastname, the other parts of the program are completely invisible for this change.
2) The access method can perform error checks. However, assigning values directly to the domain will not perform these operations. For example, the setage method can check whether the age is less than 0.
4. Note: do not compile the getter method that references the variable object. Because the method for returning a variable object will return a reference to the private member, modifications to this member can bypass the setter method, which will damage encapsulation, because encapsulation requires that the private domain of this class cannot be modified in other classes, but if a variable object reference is returned, the getter of this class can be modified by other classes, this breaks the encapsulation of the ring. To return a reference to a mutable object, clone it first ).

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

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.