"Learning Notes", "OC Language" class method

Source: Internet
Author: User

1. Basic Concepts
A method that can be executed directly with the class name (the class itself occupies storage space in memory with a list of Class \ Object methods)

2. Comparison of class methods and Object methods
1> Object Methods
Start with minus sign-
Only objects can be called, no object, and this method cannot be executed at all.
Object methods can access instance variables (member variables)

2> class method
Start with a plus + +
Can only be called with the class name, the object cannot be called
Instance variables (member variables) cannot be accessed in a class method
Use cases: When you do not need to access member variables, try to use the class method
Class methods and object methods can have the same name

3. Code

1 #import<Foundation/Foundation.h>2 /*3 Object Methods4 1> minus-Start5 2> can only be called by an object6 a member variable (instance variable) that can access the current object in the 3> object method7  8 class Method9 1> Plus + startTen 2> can only be called by Class (name) One member variables (instance variables) cannot be accessed in the 3> class method A   -   - the benefits and applications of class methods the 1> is not dependent on the object, the execution efficiency is high - 2> can use class method, try to use class method - 3> Occasions: You can change to a class method when you do not need to use a member variable inside a method -   + you can allow class methods and object methods to have the same name -  */ +  A  at @interfacePerson:nsobject - { -     intAge ; - } -  - //class methods start with A + in+ (void) Printclassname; -  to- (void) test; ++ (void) test; -  the @end *  $ @implementation PersonPanax Notoginseng  -+ (void) Printclassname the { +     //error:instance variable ' age ' accessed in class method A     //instance variable age cannot be accessed in a class method the     //NSLog (@ "This class is called person-%d", age); + } -  $- (void) Test $ { -NSLog (@"111-%d", age); -      the     //[Person test]; - }Wuyi  the+ (void) Test - { Wu     //will trigger a dead loop . -     //[Person test]; About      $NSLog (@"333"); -      -     //will trigger a dead loop . - ///[person test]; A } +  the @end -  $ intMain () the { the     //[Person Printclassname]; the      the [Person test]; -      in     //Person *p = [person new]; the     //[P test]; the      About     /* the -[person Printclassname]: Unrecognized selector sent to instance 0x7fa520c0b370 the      */ the     //The system will assume that the printclassname called now is an object method +     //[P printclassname]; -      the     return 0;Bayi}

"Learning Notes", "OC Language" class method

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.