Objective-c Access Controller
Four access controllers of member variables in objective-c:
@ Private: only the current class can be accessed internally.
@ Public: All users can access
@ Protected: only the current class and its subclass can be accessed.
@ Package: allow member variables controlled by the package to be accessed not only in the current class, but also in other programs with the same image.
What is "same image "?
Is the same framework or the same execution file generated after compilation.
That is, the current framework can be accessed, but external programs cannot. (If it is public, external programs can also access it ).
|
@ Private |
@ Package |
@ Protected |
@ Public |
Same type |
??? |
??? |
??? |
??? |
In the same image |
|
??? |
|
??? |
Subclass |
|
|
??? |
??? |
Within the global scope |
|
|
|
??? |
Example:
Interface Section: (FKApple. h)
# Import
@ Interface FKApple: NSObject
{
// Use @ package to restrict member variables
@ Package
Double _ weight;
}
@ End
Main Section:
# Import "FKApple. h"
Int main (int argc, char * argv [])
{
@ Autoreleasepool {
FKApple * apple = [[FKApple alloc] init];
Apple-& gt; _ weight = 30.4;
}
}