[Learning notes] [OC language] self keyword, learning notes ocself

Source: Internet
Author: User

[Learning notes] [OC language] self keyword, learning notes ocself

1. member variables and local variables have the same name
When the member variables and local variables have the same name, the proximity principle is adopted to access local variables.
Use self to access member variables and distinguish local variables with the same name

2. Usage Details
Where: All OC methods (Object Methods \ class methods) cannot appear in Functions
Function
Use "self-> member variable name" to access the member variables called by the current method
Use "[self method name];" to call a method (Object Method \ class method)

3. Common Errors
Low-level error: Use self to call a function
Use self to call object methods, and use self to call class methods in object Methods
Self endless loop

4. Code

1.

1 # import <Foundation/Foundation. h> 2 3 @ interface Person: NSObject 4 {5 int _ age; 6} 7 8-(void) setAge :( int) age; 9-(int) age; 10 11-(void) test; 12 13 @ end14 15 @ implementation Person16-(void) setAge :( int) age17 {18 // _ age = age; 19 self-> _ age = age; 20} 21-(int) age22 {23 return self-> _ age; 24} 25 26-(void) test27 {28 // self: directed to the caller, representing the current object 29 int _ age = 20; 30 NSLog (@ "Person's age is % d ", self-> _ age); 31} 32 33 @ end34 35 int main () 36 {37 Person * p = [Person new]; 38 39 [p setAge: 10]; 40 41 [p test]; 42 43 return 0; 44}

2.

1 # import <Foundation/Foundation. h> 2 3/* 4 self purpose: 5 1> who calls the current method, self indicates who 6 * self appears in the object method, self indicates that the object 7 * self appears in the class method, self indicates class 8 9 2> using "self-> member variable name" in the object method to access the member variable inside the current object 10 11 2> [self method name] can call other objects method \ Class Method 12 */13 14 @ interface Dog: NSObject15-(void) bark; 16-(void) run; 17 @ end18 19 @ implementation Dog20-(void) bark21 {22 NSLog (@ "Wang "); 23} 24-(void) run25 {26 [self bark]; 27 // NSLog (@ "Wang"); 28 NSLog (@ "running "); 29} 30 @ end31 32 int main () 33 {34 Dog * d = [Dog new]; 35 36 [d run]; 37 38 return 0; 39}

3.

1/* 2 design a calculator Class 3 * sum 4 * calculate the average value 5 */6 7 # import <Foundation/Foundation. h> 8 9 // tool class: basically no member variables, the methods in it are basically class Methods 10 @ interface JiSusnQi: NSObject11 + (int) sumOfNum1 :( int) num1 andNum2 :( int) num2; 12 13 + (int) averageOfNum1 :( int) num1 andNum2 :( int) num2; 14 @ end15 16 @ implementation JiSusnQi17 + (int) sumOfNum1 :( int) num1 andNum2 :( int) num218 {19 return num1 + num2; 20} 21 22 + (int) averageOfNum1 :( int) num1 andNum2 :( int) num223 {24 // in this case, self represents the 25 int sum = [self sumOfNum1: num1 andNum2: num2]; 26 return sum/2; 27} 28 @ end29 30 int main () 31 {32 int a = [JiSusnQi averageOfNum1: 10 andNum2: 12]; 33 34 NSLog (@ "a = % d", ); 35 36 // JiSusnQi * jsq = [JiSusnQi new]; 37 // 38 // 39 // [jsq sumOfNum1: 10 andNum2: 13]; 40 41 return 0; 42}

4. Usage notes

1 # import <Foundation/Foundation. h> 2 3 @ interface Person: NSObject 4-(void) test; 5 + (void) test; 6 7-(void) test1; 8 + (void) test2; 9 10 11-(void) haha1; 12 13 + (void) haha2; 14 15 16 @ end17 18 @ implementation Person19-(void) test20 {21 NSLog (@ "-test method called"); 22 23 // will cause an endless loop 24 // [self test]; 25} 26 27 + (void) test28 {29 NSLog (@ "+ test method called"); 30 31 // will lead to an endless loop 32 // [self test]; 33} 34 35-(void) test136 {37 [self test]; //-test38} 39 40 + (void) test241 {42 [self test]; // + test43} 44 45-(void) haha146 {47 NSLog (@ "haha1 -----"); 48} 49 50 51 void hahaha3 () 52 {53 54} 55 56 + (void) haha257 {58 // haha3 (); 59 [self haha3]; 60 // [self haha1]; 61} 62 @ end63 64 int main () 65 {66 [Person haha2]; 67 // Person * p = [Person new]; 68 69 // [p test1]; 70 return 0; 71}

 

 

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.