IOS Development Learning SUMMARY objective-c object-oriented-encapsulation and Access Control Operators

Source: Internet
Author: User

IOS Development Learning SUMMARY objective-c object-oriented-encapsulation and Access Control Operators
Encapsulation Definition

Object-oriented features (encapsulation, inheritance, polymorphism.It refers:Hiding the object state information inside the object does not allow external programs to directly access the internal information of the object. Instead, the internal information is operated and accessed through the methods provided by this class.

Good encapsulation for the purpose

Good encapsulation of a class or object can be achieved for the following purposes:
1. Hide the implementation details of the class;
2. the user can only access data through pre-defined methods, so that the control logic can be added to this method to restrict unreasonable access to member variables.
3. Data check is supported to ensure the integrity of object information;
4. Easy to modify. Improve code maintainability.

How to implement good encapsulation to hide the member variables and implementation details of an object, and direct external access is not allowed. Expose methods to control secure access and operations on these member variables. Access Controller

Encapsulate the four access controllers that need to be provided by objective-c: @ private, @ package, @ protected, and @ public. They represent four access control levels.
Their access control levels are in ascending order, for example:

The four access controllers are described as follows:
1. @ private: [current class access permission], which can only be accessed within the current class. Used to completely hide member variables. The member variables defined in the implementation part of the class are equivalent to the default access permission.
2. @ package: [the same as the image access permission] is used to hide some member variables. If the member variables of the class are restricted by the @ package access controller, the member can be accessed anywhere in the same image implemented by the current class and the current class.
3. @ protected: [subclass access permission] is used to hide some member variables. If the member variables of the class are restricted by the @ protected access controller, the member can be accessed anywhere in the current class and the subclass of the current class.This access permission is used by default for the member variables defined in the interface section of the class. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Examples/examples ptpazazsu9ozz/HW0CzSsrK7udzKx7fxvt/T0LzMs9C52M + Examples "here write picture description" src = "http://www.bkjia.com/uploads/allimg/151012/0516024627-1.png" title = "\"/>
So can local variables be modified by access controllers?
The answer is no. Because for a local variable, its scope is its method, and it cannot be accessed by other classes. Therefore, it cannot be modified with an access controller. While The access controller is used to control whether the member variables of the class can be accessed by other classes.
Example program:
FKPerson. h file

# Import  @ Interface FKPerson: NSObject {// use @ private to restrict member variables @ private NSString * _ name; int _ age;} // provides a method to operate name Field-(void) setName: (NSString *) name; // provides a method to obtain the value of the _ name member variable-(NSString *) name; // provides a method to set the age member variable-(void) setAge: (int) age; // provides a method to obtain the value of the _ age member variable-(int) age; @ end 

FKPerson. m file

# Import FKPerson. h @ implementation FKPerson // provides a method to set the _ name member variable-(void) setName: (NSString *) name {// perform the rationality check. The username must be 2 ~ If ([name length]> 6 | [name length] <2) {NSLog (@ the name you set does not meet the requirements); return ;} else {_ name = name ;}}// provides a method to obtain the value of the _ name member variable-(NSString *) name {return _ name ;} // provides a method to set the age member variable-(void) setAge :( int) age {if (_ age! = Age) {// perform a rationality check. The user's age must be between 0 and ~ Between 100 if (age> 100 | age <0) {NSLog (@ your age is invalid); return ;}else {_ age = age ;}}} // provides a method to obtain the value of the _ age member variable-(int) age {return _ age;} @ end

[Knowledge cut-in]How is the range of access control operators divided?
@ Private, @ package, @ protected, and @ public. The range of these four permission controllers is as follows, starting from where they appear, the member variables between the next permission control character or the braces on the right are all controlled by the access control character.
The setter and getter methods of member variables in oc are of great significance. For example, if a class contains a Field named _ sun, the corresponding setter and getter method names should be the first letter of the member variable name in setSun and sun, add the set verb to the beginning, and the getter method name removes the line prefix)
Example program:

# Import FKPerson. hint main (int argc, char * argv []) {@ autoreleasepool {FKPerson * p = [[FKPerson alloc] init]; // because the age member variable has been hidden, therefore, the following statement will cause a compilation error. // P-> _ age = 1000; // The following statement compilation will not cause errors, however, the error message "invalid age member variable" appears during running. // the program will not modify the age member variable [p setAge: 1000] of p. // The age member variable for accessing p must also use its corresponding getter method // because the age member variable of p has never been successfully set above, therefore, 0 NSLog is output (@ when the age member variable is not set: % d, [p age]); // The age member variable of p is successfully modified [p setAge: 30]; // because the age member variable of p is successfully set above, 30 NSLog is output (@ after the age member variable is successfully set: % d, [p age]); // you cannot directly operate the name member variable of p. You can only use the corresponding setter method. // because the length of the Li Gang string is 2 ~ 6, so you can successfully set [p setName: @ Li Gang]; NSLog (@ after successfully setting the name member variable: % @, [p name]);}

The FKperson object can no longer directly modify its member variables. Only the corresponding setter and getter methods can be used to set the value of member variables. When the setter method sets the response member variable, the programmer is allowed to add his/her own control logic in the setter method to ensure that these member variables do not conform to the actual situation.

Most of the member variables in the basic principle class used by the Access Controller should use the @ private restriction.
The tool method (that is, the method that only helps to implement other methods of the class) should be hidden inside the class. In this case, the methods should be defined as the implementation part of the class. If a class is mainly used as the parent class of other classes, which contains member variables and wants to be accessed by the quilt class, you can use @ protected to restrict these member variables. Methods exposed to other classes that can be freely called should be defined in the class interface and then implemented in the class implementation part. Understanding @ package

Member variables restricted by @ package can be accessed in the current class or in other programs with the same image. So:What is "same image?
The same image, in short, is the same framework or execution file generated after compilation.
For example, if you want to develop a basic framework, if you use @ private to restrict member variables, it is too restrictive-consider other classes in the framework. Other functions may need to directly access this member variable, however, this framework does not want other external programs to access this member variable. In this case, @ package is used to restrict member variables.
When the compiler compiles the class where the member variables restricted by @ package are located and other classes and functions into a framework library, all these classes and functions are in the same image, all functions can freely access member variables restricted by @ package. However, when other programs reference this Framework library, because other programs only rely on this framework library, other programs are not in the same image as this Framework library. Therefore, other programs cannot access the member variables restricted by this @ package.
Example program:
FKApple. h file

# Import
   
    
@ Interface FKApple: NSObject {// use @ package to restrict member variables @ package double _ weight;} @ end
   

FKApple. m file

#import FKApple.h@implementation FKApple@end

FKAppleTest. m file

# Import FKApple. hint main (int argc, char * argv []) {@ autoreleasepool {FKApple * apple = [FKApple alloc] init]; // The following program directly accesses the member variable restricted by @ package, and the value is successfully assigned to apple-> _ weight = 30.4; NSLog (@ apple weight: % g, apple-> _ weight );}}

After compiling, the compiler will. h, FKApple. m, FKAppleTest. the m file is compiled into an execution file. Because the FKApple class and the main () function are in the same image, the main () function can freely access the member variables restricted by @ package in the FKApple class.

 

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.