------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------
One, dot syntax
The purpose of OC design point syntax is to enable other language developers to quickly get started with the OC language, OC Point syntax is very similar to Java, what is the point of grammar, how to use the dot grammar?
1, point syntax and set and get methods related
Create a new project here and add a person class
Person.h
1 //2 //Person.h3 //Zijia4 //5 //Created by Zou on 5/10/15.6 //Copyright (c) __mycompanyname__. All rights reserved.7 //8 9 #import<Foundation/Foundation.h>Ten One @interfacePerson:nsobject A { - int_age; - the } - -- (void) Setage: (int) age; -- (int) age; + @end
Person.m
1 //2 //person.m3 //Zijia4 //5 //Created by Zou on 5/10/15.6 //Copyright (c) __mycompanyname__. All rights reserved.7 //8 9 #import "Person.h"Ten One @implementation Person A- (void) Setage: (int) Age - { -_age =Age ; the } -- (int) Age - { - return_age; + } - @end
Main.m
1 //2 //main.m3 //Zijia4 //5 //Created by Zou on 5/9/15.6 //Copyright (c) __mycompanyname__. All rights reserved.7 //8 9 #import<Foundation/Foundation.h>Ten #import "Person.h" One A intMainintargcConst Char*argv[]) - { - the @autoreleasepool { - -Person *p = [personNew]; -[P Setage:Ten]; + intA =[P age]; -NSLog (@"Age is %d", a); + A } at return 0; -}
Here is a set of get methods to change and get the value of the member variable, but it looks a bit strange, you can use the point syntax instead.
Ten // [P setage:10];
The nature of point syntax is still a method invocation, not a member variable, and the Access member variable method should be: p->age=10. Here the syntax calls the Set method, how does the Get method be implemented with DOT syntax?
int // int a = [P age];
What do you need to be aware of when using point syntax, see the following code:
Person.m
-(void) Setage: (int) age{ // _age = age; NSLog (@ "setage-"); = Age ;}
Here in the Set method, _age = can age use dot syntax self.age = age instead?
Self.age is equivalent to calling the Setage method [self setage:age], so that the program calls the Setage method, and the program goes into a dead loop.
Print output:
If you write return Self.age in the Get method implementation, the program also enters the dead loop.
-(int) age{ NSLog (@ "age-"); return self.age; // return _age; }@end
Execute code:
Here again, the point syntax essence is to call the Set method. If there is a syntax in the program without the set and get methods, the program will error.
Second, member variable scope
Member variable scopes are divided into four types:
1, @private: can only be accessed directly in the implementation @implementation of the current class
2. @protected: can be accessed directly in the implementation @implementation of the current class and subclass
3, @public: Direct access to any place
4, @package: Within the same "system" (framework) can be accessed between @private and @public
When you precede a member variable with a keyword, the scope of the member variable changes.
Can subclasses use the private member variables of the parent class? The answer is no, you can only access the parent class's private member variable by calling the parent class's set and get methods.
If the keyword is not written, the member variable defaults to @protected, which can only be accessed by the implementation portion of the current class and subclass.
Member variables that are defined in the implementation section are private by default.
Dark Horse Programmer---objective-c basic Learning---Point method usage and scope of member variables