OC point syntax and variable scope
I. Point syntax
(1) Recognition point syntax
Declare a person class:
1 # import <Foundation/Foundation. h> 2 3 @ interface person: nsobject 4 {5 Int _ age; // The default value is @ protected 6} 7 8-(void) setage :( INT) age; 9-(INT) age; 10 11 @ end
Implementation of the person class:
1 # import "person. H "2 3 @ implementation person 4 5-(void) setage :( INT) Age 6 {7 _ age = age; // cannot be written as self. age = newage, equivalent to [self setage: newage]; 8} 9 10-(INT) Age // get method 11 {12 Return _ age; 13} 14 15 @ end
Point Syntax:
1 # import <Foundation/Foundation. h> 2 # import "person. H "3 4 int main (INT argc, const char * argv []) 5 {6 7 @ autoreleasepool {8 9 // insert code here... 10 person * person = [[person alloc] init]; 11 12 // [person setage: 10]; 13 person. age = 10; // point syntax, which is equivalent to [person setage: 10]; 14 // the attribute of person is not assigned here, instead, call the setage method of person 15 16 // int age = [person age]; 17 int age = person. age; // equivalent to int age = [person age] 18 nslog (@ "Age is % I", age); 19 [person release]; 20 21} 22 return 0; 23}
(2) functions of point syntax
The objective of the OC design point syntax is to enable developers in other languages to quickly get started with the development of the OC language and use the point syntax to make it very similar to other object-oriented languages such as Java.
(3) essence of point syntax
The essence of point syntax is method calling, rather than accessing member variables. When using point syntax, the compiler automatically expands to the corresponding method. Note that the essence of point syntax is to convert it into the corresponding set and get methods. If there is no set or get method, point syntax cannot be used.
For example:
Stu. Age = 10; expand to [STU setage: 10];
Int A = Stu. Age; expand to [STU age];
How does the compiler know whether it is the set or get method? It mainly refers to the value assignment (you can use breakpoint debugging to view the value ).
There is only one way to access a member variable in OC, that is, to use a variable such as-> stu-> age. In this case, @ public is required.
(4) usage of point syntax
The following is an endless loop:
(1) In the set method, self. Age = age; equivalent to [self setage: Age];
(2) In the get method, return self. Age; equivalent to [self age];
Ii. variable scope
(1) There are four main scopes of variables:
(1) @ public (public) can be directly accessed anywhere on the premise of 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: the framework-level scope is between private and public. You can directly access the package by using the variable name as long as it is in the same framework.
(2) usage notes and supplements
(1) The implementation of the class is. member variables can also be declared in the M file, but because other files generally only contain header files but not implementation files, the member variables declared here are @ private. The member variables defined in. m cannot drink the same name as the member variables in the header file. H. During this period, using keywords such as @ public is futile.
(2) If the member variables declared between @ interface @ end are not specified, the default value is protected.
(3) If a class inherits from another class, it owns all the member variables and methods of the parent class. Note that all member variables have them, but some cannot be directly accessed.