"OC" Point syntax and member variable scope, js variable scope
I. Point syntax(1) Recognition point syntaxDeclare a Person class:
1 #import <Foundation/Foundation.h> 2 3 @interface Person : NSObject 4 { 5 int _age; 6 NSString *_name; 7 } 8 9 - (void)setAge:(int)age;10 - (int)age;11 12 - (void)setName:(NSString *)name;13 - (NSString *)name;14 15 @end
Implementation of the Person class:
1 # import "Person. h "2 3 @ implementation Person 4-(void) setAge :( int) age 5 {6 _ age = age; 7 NSLog (@" setAge "); 8 9 // will lead to an endless loop 10 // self. age = age; // [self setAge: age]; 11} 12-(int) age13 {14 NSLog (@ "age"); 15 return self-> _ age; 16 17 // will cause an infinite loop 18 // return self. age; // [self age]; 19} 20-(void) setName :( NSString *) name21 {22 _ name = name; 23} 24-(NSString *) name25 {26 return _ name; 27} 28 @ end
Point Syntax:
1 # import <Foundation/Foundation. h> 2 # import "Person. h "3 4 int main (int argc, const char * argv []) 5 {6 Person * p = [Person new]; 7 8 // the essence of the Point syntax is the method call 9 p. age = 10; // [p setAge: 10]; 10 int a = p. age; // [p age]; 11 12 NSLog (@ "% d", a); 13 14 p. name = @ "Jack"; 15 NSString * s = p. name; 16 17 NSLog (@ "% @", s); 18 19 return 0; 20}
(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 the dot syntax is used, the compiler automatically expands to the corresponding method.
Actually, it is converted to the corresponding setter and getter. If there is no setter or getter, the dot 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.
(3) code Demonstration: 1 # import <Foundation/Foundation. h> 2 3 @ interface Person: NSObject 4 {5 int _ no; 6 7 @ public // The member variable 8 int _ age of the object can be directly accessed anywhere; 9 10 11 @ private // you can only directly access 12 int _ height in the object method of the current class; 13 14 @ protected // you can directly access 15 int _ weight; 16 int _ money; 17} 18 19-(void) setHeight :( int) in the object methods of the current class and subclass) height; 20-(int) height; 21 22-(void) test; 23 @ endPerson. h 1 # import "Person. h "2 3 @ implementation Person 4 {5 int _ aaa; // The default value is private 6 7 @ public 8 int _ bbb; 9 // In @ implementation, the member variable 10 with the same name as @ interface cannot be defined. // int _ no; 11} 12 13-(void) test14 {15 _ age = 19; 16 17 _ height = 20; 18 19 _ weight = 50; 20 21 _ aaa = 10; 22} 23 24-(void) setHeight :( int) height25 {26 _ height = height; 27} 28 29-(int) height30 {31 return _ height; 32} 33 34 @ endPerson. m1 # import "Person. h" 2 3 @ interface Student: Person4-(void) study; 5 @ endStudent. h 1 # import "Student. h "2 3 @ implementation Student 4-(void) study 5 {6 7 // _ height = 10; 8 [self setHeight: 10]; 9 10 11 int h = [self height]; 12 13 _ weight = 100; 14} 15 @ endStudent. m main. m
1 #import <Foundation/Foundation.h> 2 #import "Person.h" 3 #import "Student.h" 4 5 @implementation Car : NSObject 6 { 7 @public 8 int _speed; 9 10 @protected11 int _wheels;12 }13 14 - (void)setSpeed:(int)speed15 {16 _speed = speed;17 }18 - (int)speed19 {20 return _speed;21 }22 23 @end24 25 int main(int argc, const char * argv[])26 {27 28 @autoreleasepool {29 Student *stu = [Student new];30 31 32 [stu setHeight:100];33 34 35 NSLog(@"%d", [stu height]);36 37 /*38 Car *c = [Car new];39 c->_speed = 250;40 */41 //c.speed = 10;42 43 // NSLog(@"%d", c.speed);44 45 //[c setSpeed:<#(int)#>];46 47 /*48 Person *p = [Person new];49 p->_bbb = 10;50 p->_age = 100;51 */52 //p->_height = 20;53 54 55 //p->_weight = 10;56 }57 return 0;58 }