Objective-CMediumMethodAndObjectThis document introduces the user guide.Objective-CPublic, protected, and private. Same as C ++,Objective-CThe use of public, protected, and private also exists, but the forms are different. Here is a brief summary.
1. @ public, @ protected, @ private
I personally think there is no clear difference between @ public and @ protected.ObjectIf you do not set the object to @ property, the object is equivalent to a protected object. Only the subclass and itself can access this object. If you set the object to @ property, the user can access this object. @ Private: As the name implies, private objects are declared here.
Note: It can be accessed through pointers.
2. static object class)
We declare the object in the class "{" and "}", that isMethodWith @ property, you can declare a static object inside the class.
3. static Method
When "+" is used to modify the method and declare it in the header file, it means that this method is equivalent to the static method in c ++ and called directly through the class. However, although such a method can be called directly through a class, it cannot be called through an object.
4. public Method
When "-" is used before the method and is declared in the header file, the method can be called through the class object.
5. private Method
In Objective-C, the private method is implemented through category. In the implementation file, we declare the category of A Class. Here, the private method is used. Class objects cannot be called. Similarly, because the method is known in the class implementation file, subclass cannot override this method.
The following code implements public, protected, and private.
Header file
- # Import <Foundation/Foundation. h>
- @ Interface Grammar: NSObject {
- @ Public
- NSString * publicString;
- @ Protected
- NSString * protectedString;
- @ Private
- NSString * privateString;
- }
- NSString * staticString;
- @ Property (nonatomic, retain) NSString * publicString;
- + (Void) staticMethod;
- -(Void) publicMethod;
- @ End
- Implementation File
- # Import "Grammar. h"
-
- # Pragma mark-
- # Pragma mark Grammar (private)
- @ Interface Grammar (private)
- -(Void) privateMethod;
- @ End
- # Pragma mark-
- # Pragma mark Grammar
- @ Implementation Grammar
- @ Synthesize publicString;
-
- -(Id) init
- {
- If (self = [super init])
- {
- If (publicString = nil)
- {
- PublicString = [[NSString alloc] init];
- }
- If (protectedString = nil)
- {
- ProtectedString = [[NSString alloc] init];
- }
- If (privateString = nil)
- {
- PrivateString = [[NSString alloc] init];
- }
- If (staticString = nil)
- {
- StaticString = [[NSString alloc] init];
- }
- }
- Return self;
- }
- -(Void) dealloc
- {
- [PublicString release];
- [ProtectedString release];
- [PrivateString release];
- [Super dealloc];
- }
- # Pragma mark-
- # Pragma mark Public Method
- + (Void) staticMethod
- {
- }
- -(Void) publicMethod
- {
- }
- # Pragma mark-
- # Pragma mark Private Method
- -(Void) privateMethod
- {
- }
- @ End
The above is my understanding of public, protected, and private in Objective-C. If there is any new understanding, it will be updated.
Summary:Objective-CMediumMethodAndObjectThe User Guide is complete. I hope this article will help you!