Objective-C Chinese method and Object User Guide

Source: Internet
Author: User

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

 
 
  1. # Import <Foundation/Foundation. h>
  2. @ Interface Grammar: NSObject {
  3. @ Public
  4. NSString * publicString;
  5. @ Protected
  6. NSString * protectedString;
  7. @ Private
  8. NSString * privateString;
  9. }
  10. NSString * staticString;
  11. @ Property (nonatomic, retain) NSString * publicString;
  12. + (Void) staticMethod;
  13. -(Void) publicMethod;
  14. @ End
  15. Implementation File
  16. # Import "Grammar. h"
  17.  
  18. # Pragma mark-
  19. # Pragma mark Grammar (private)
  20. @ Interface Grammar (private)
  21. -(Void) privateMethod;
  22. @ End
  23. # Pragma mark-
  24. # Pragma mark Grammar
  25. @ Implementation Grammar
  26. @ Synthesize publicString;
  27.  
  28. -(Id) init
  29. {
  30. If (self = [super init])
  31. {
  32. If (publicString = nil)
  33. {
  34. PublicString = [[NSString alloc] init];
  35. }
  36. If (protectedString = nil)
  37. {
  38. ProtectedString = [[NSString alloc] init];
  39. }
  40. If (privateString = nil)
  41. {
  42. PrivateString = [[NSString alloc] init];
  43. }
  44. If (staticString = nil)
  45. {
  46. StaticString = [[NSString alloc] init];
  47. }
  48. }
  49. Return self;
  50. }
  51. -(Void) dealloc
  52. {
  53. [PublicString release];
  54. [ProtectedString release];
  55. [PrivateString release];
  56. [Super dealloc];
  57. }
  58. # Pragma mark-
  59. # Pragma mark Public Method
  60. + (Void) staticMethod
  61. {
  62. }
  63. -(Void) publicMethod
  64. {
  65. }
  66. # Pragma mark-
  67. # Pragma mark Private Method
  68. -(Void) privateMethod
  69. {
  70. }
  71. @ 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!

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.