A. dot Syntax The essence of point syntax is method invocation , not access member variable, compiler sees point syntax has two kinds of processing
1 . is an assignment, the assignment is to expand to the set method
2. Not assignment: expand to set method
Examples of Use:
Declaration of the Person class:
Test of Point syntax:
Two. Scope of the OC member variable
@public: Anywhere access @private: @protected can only be accessed directly in the object method of the current class: it is accessible directly in the current class and subclass (default) @package: Object member variables can be accessed directly in the same frame
Three. @property and @synthesize
Because of the encapsulation of the class, we are required to provide some external methods to access our member variables, which is the set or Get method in OC, but because the set or get method format is fixed, and if the member variable too much, write it takes time and not much help, So we have the following two keywords to help us complete the set and get methods:1. @property1) used in @interface 2) to automatically generate setter and Getter3) @propertyint age; You can replace the following two lines-(void) Setaage: (int) age; -(int) age;2. @synthesize1> used in @implementation 2> to automatically generate automatic generation of set and get methods implemented 3> with @synthesize age = _age; can replace
1 -(int) Age2{3 eturn _age; 4 }5 -(void) Setage: (int)age6{7 _age =8 }
Description: Xcode4. After a few @property @synthesize function is @property can generate both the set and get method declaration and implementation, member variables are not declared, the disadvantage is that the member variable is private, you have to add member variable protect @ Property Usage Example: Declaration of the Person class:
1 #import<Foundation/Foundation.h>2 3 @interfacePerson:nsobject4 5 //@property can automatically generate a set and get declaration for a member6 //member variable name cannot be added _7 //If the member variable does not exist, the private type is automatically generated8@propertyintAge ; 9 //-(void) Setaage: (int) age; Ten //-(int) age; One @end
Implementation of the person class:
Test:
#import<Foundation/Foundation.h>#import "Person.h" intMainintargcConstCharchar *argv[]) { person*p = [PersonNew]; /*use of point syntax*/P.age=5; NSLog (@"P.age%d", P.age); return 0; }
SET[762:52529] P.age 5
Manual implementation of the situation1) If the Set method is implemented manually, the compiler will only automatically generate a Get Method 2) If the Get method is implemented manually, the compiler will only automatically generate the Set Method 3) If the set method and the Get method are implemented manually, the compiler does not automatically generate a non-existent member variable principle: You have it, I won't do it for you. You implement get I just generate set, and if you implement set and get, no member variables are generated.
Four. Introduction of ID:1. Universal pointer, pointing to any OC object, equivalent to NSObject. ID type definition
1 struct Object {2 Class Isa; 3 }*ID;
Use example: id p = [person new]; limitations: Call a non-existent method and the compiler immediately errors.Five. OC Construction method:1. What is a construction method, what is the construction method used for, and why is there a construction method? The constructor method is the method used to initialize the object, starting with the object created with the new method, the initial value of the member of the object is 0, which is inconvenient in development, often to initialize the value of some properties when the object is created, so this construction method can be used to meet this requirement. 2. The process of creating objects using the system's own new: [Person new]; Complete creation of an available object, new do what, call two Method 1. Allocate storage space (an object) +alloc 2. Initialize the-init method to complete initialization, which is usually the member The property is set to 0. 3. To create an object yourself without using NEW:
/** //person*p1 = [Person alloc] ; /* * //person*p2 = [P1 init]; // just a word. Common in development , initialize your own values Person *P3 = [[Person alloc] init];
4. How do I use the construction method to set the initial value of an object member? There is a requirement: Each person object is created, his _age are 10; idea one: Overriding the-init method: Overriding the-init Method Example: the definition of the person class
#import <Foundation/Foundation.h> @interface person:nsobject int age ; @end
The implementation of the person class overrides the parent class's Init method, initializing _age to 10
1 @implementation Person2 3 /*overriding the-init method*/ 4- (ID) Init5 { 6 //1. Be sure to call Super's Init method: Initialize some member variables declared in the parent class7 8 //2. The subsequent initialization is necessary if the object is initialized successfully9 //to ensure that the return is correct after initializationTen if(self =[Super init]) One{//Initialization succeeded A_age =Ten; - } - the //3. Returns an object that has already been initialized - returnSelf ; - } - + @end
Test code:
1 int main () 2 3 /* need to create each person object, his _age are all */ 4 Person *p = [[Person alloc] init]; 5 NSLog (@ " p Age%d " ,p.age,); 6 return 0 ; 7 }
2015-03-23 14:09:43.375 Construction Method [917:81284] P age
Idea two: Custom construction methods,Custom Construction methods: two parts-declaration and implementationSpecification for custom construction methods: 1. Must be object Method 2. The return value is ID type 3. The method name starts with Init using the example:
Declaration of the person class: Custom constructor method user settings name and age
#import <Foundation/Foundation.h> Person:nsobject {nsstring *_ Name int _age; } @property nsstring *name; @property int age; // Custom Construction method: two-part declaration and implementation /* Custom construction method specification: 1. Must be an object Method 2. The return value is the ID Type 3. Method name starts with init */-(id ) Initwithname: (NSString *) name Andage: (int ) age; @end
Person class implementation
#import " Person.h " @implementation person -(ID) Initwithname: (NSString *) name andage: (int) Age { /* * /if(self = [super init]) { = name ; = Age ; } return Self ; } @end
Test code:
#import "Person.h" #import "Student.h" intMain () {/*Initialize the name and age of a person object using a custom construction method*/ Person*p = [[Person alloc] Initwithname:@"Mike"Andage: -]; NSLog (@"name%@ Age%d", P.name,p.age); return 0;
}
2015-03-23 14:20:15.727 Custom Construction method [966:85096] name Mike age
Summary: The constructor method is used to initialize the member variables, and the member variables belong to the parent class and are initialized in the parent class.
Objective-c Basic Learning Notes (vi)-property action-constructor method-oc the scope of member variables