OC point syntax and variable scope
One, dot syntax
(i) Cognitive point grammar
Declare a person class:
1#import <Foundation/Foundation.h> 3 @interface Person:nsobject 4 { 5. Span style= "color: #0000ff;" >int _age; // 6 Span style= "color: #000000;" >} 7 8-(void) Setage: ( Span style= "color: #0000ff;" >int) age; 9-(int10 11 @end
implementation of the person class:
1#import"Person.h"23@implementationPerson45-(void) Setage: (Int) Age 6 { 7 _age = age; // can't write self.age = NewAge, quite with [self setage:newage]; 8 } 9 10-(int) Age //11 Span style= "color: #000000;" >{12 return _age;13 }14 15 @end
Use of point syntax:
1#import <Foundation/Foundation.h>2#import"Person.h"34int main (int argc,Constchar *Argv[])5{67@autoreleasepool {89//Insert code here ...Ten person *person =[[Person alloc] init];1112//[Person Setage:10];Person.age =10;//Point syntax, equivalent to [person setage:10];14//Instead of assigning a value to a person's property, this is called the Setage method of the person1516 //int age = [person Age]; 17 int age = Person.age; equivalent to int age = [person Age]18 NSLog (@ "age is%i" 19 [person Release];21 }22 return 0;
(ii) The role of Dot grammar
OC Design Point syntax is designed to enable developers of other languages to quickly get started with OC language development, using DOT syntax, which is similar to other object-oriented languages like Java .
(c) The nature of Point grammar
The essence of point syntax is the invocation of the method, not the access to the member variable, and the compiler automatically expands to the appropriate method when the point syntax is used. Remember that the essence of point syntax is to convert to the appropriate set and get methods, and you cannot use point syntax without the set and get methods.
Such as:
stu.age=10; unfolded as:[Stu Setage:10];
int a=stu.age; unfolded as:[Stu Age];
How does the compiler know if it is a set method or a get method? The main point is to look at the assignment (you can use breakpoint debugging to see it).
There is only one way to access member variables in OC , such as Stu->age, which isrequired under @public condition .
(d) Use of point grammar note
The following usage is a dead loop:
(1) In the Set method, Self.age=age; equivalent to [self setage:age];
(2) in the Get method, return self.age;
Ii. Scope of variables
(a) The scope of the variable is divided into four main types:
(1) @public ( public ) can be accessed directly from anywhere with an object.
(2) @protected (protected) can only be accessed in the object methods of the current class and subclass
(3) @private (private) can only be accessed directly in the object method of the current class
(4) @package ( frame-level ) scopes are between private and public, and can be accessed directly from the variable name as long as they are in the same frame
(ii) Use of attention and supplement
1 .m @private "Cannot drink its header file .h @public and other keywords are in vain.
(2) member variables declared between @interface @end are protected by default if they are not specifically described .
(3) When one class inherits another, it has all the member variables and methods of the parent class, noting that all the member variables have it, but that it does not directly access
OC point syntax and variable scope