Objective-c Study Notes (20)--analysis of the visibility of member variables on object methods and class methods

Source: Internet
Author: User



Prior to learning C + +, or Java, there was a visibility issue, and this article discussed the visibility of declared member variables on object methods (minus methods) and class methods (plus methods).



(a) Code one: member variables are visible to the minus method, not visible to the plus method.



Declare a member variable in PEOPLE.M: NSString *_peoplename, and then find that the variable can be accessed in the minus method and not accessible in the Plus method.





-(void) ObjectShow {

     NSLog (@ "I am an object method");
     [email protected] "member variables"; // You can access member variables here;
    
}

+ (void) ClassShow {

     NSLog (@ "I am a class method");
     // Cannot access _peopleName here;
    
}


(b) Code two: static member variables are visible to the minus method, and to the plus method.





Use static (familiar with Java-learning) keywords in people.m to declare a static variable: static NSString *_peoplename2; You can then discover that the static variable can be accessed both in the minus and plus methods.





-(void) ObjectShow {

     NSLog (@ "I am an object method");
     [email protected] "Jack"; // A static variable was accessed in the object method;
    
}

+ (void) ClassShow {

     NSLog (@ "I am a class method");
     [email protected] "Mike"; // A static variable was accessed in the class method;

    
} 



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.



Objective-c Study Notes (20)--analysis of the visibility of member variables on object methods and class methods


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.