[Learning notes] [OC language] class method, learning notes oc Language

Source: Internet
Author: User

[Learning notes] [OC language] class method, learning notes oc Language

1. Basic Concepts
Methods that can be directly executed using class names (Classes occupy storage space in the memory, including the class \ object method List)

2. Comparison between class methods and object Methods
1> Object Method
Start with minus sign-
This method can only be called by objects without objects.
Object methods can access instance variables (member variables)

2> class methods
Starting with a plus sign +
It can only be called by class name, and the object cannot be called
Instance variables (member variables) cannot be accessed in class methods)
Usage scenarios: When you do not need to access member variables, use the class method whenever possible.
Class methods and object methods can have the same name

3. Code

1 # import <Foundation/Foundation. h> 2/* 3 object method 4 1> minus sign-start 5 2> only objects can call 6 3> Object methods can access member variables of the current object (instance variables) 7 8 class Methods 9 1> plus sign + start 10 2> only class (name) calls 11 3> member variables (instance variables) cannot be accessed in class methods) 12 13 advantages and Use Cases of Class 14 Methods 15 1> independent of objects, high execution efficiency 16 2> use class methods and try to use class Methods 17 3> occasions: if you do not need to use member variables inside the method, you can change it to class Method 18 19. You can allow class methods and object methods to have the same name 20 */21 22 23 @ interface Person: NSObject24 {25 int age; 26} 27 28 // all class methods start with + 29 + (void) printClassName; 30 31-(void) test; 32 + (void) test; 33 34 @ end35 36 @ implementation Person37 38 + (void) printClassName39 {40 // error: instance variable 'age' accessed in class method41 // The instance variable age cannot access 42 // NSLog in class methods (@ "this class is called Person-% d", age ); 43} 44 45-(void) test46 {47 NSLog (@" 111-% d ", age); 48 49 // [Person test]; 50} 51 52 + (void) test53 {54 // will lead to an endless loop 55 // [Person test]; 56 57 NSLog (@ "333 "); 58 59 // will cause an endless loop 60 // [Person test]; 61} 62 63 @ end64 65 int main () 66 {67 // [Person printClassName]; 68 69 [Person test]; 70 71 // Person * p = [Person new]; 72 // [p test]; 73 74/* 75-[Person printClassName]: unrecognized selector sent to instance 0x7fa520c0b37076 */77 // The system will think that the currently called printClassName is an object Method 78 // [p printClassName]; 79 80 return 0; 81}

 

 

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.