1 Synthetic access Methods
It is not difficult to implement setter and getter methods for member variables, but if a class contains 10 or more member variables, writing setter and Getter methods for each member will be a disgusting thing. The OC language provides automatic synthesis of setter and getter methods, and developers can still provide setter and getter methods if they need to control the implementation of a setter and getter method. This developer-defined setter and getter method will override the system's auto-synthesized setter and getter methods.
The system automatically synthesizes setter and Getter methods as follows:
1. Use the @property directive in the Declarations section of the class, such as @property int age; The system automatically generates the following statement:
-(void) Setage: (int) age;
-(int) age;
2, in the implementation of the class using @synthesize instructions, such as @synthesize age = _age, where _age is a member variable in the Declaration section, the system will automatically implement setter and getter method
#import <Foundation/Foundation.h> #import "Person.h" int main () {person *p = [person new]; P.age =; int c = p.age; P.name = @ "Guopeng"; NSString *GP = p.name; NSLog (@ "%d%@", C,GP); return 0;}
#import <Foundation/Foundation.h> @interface person:nsobject{ int _age; NSString *_name;} -(void) Setage: (int) age;-(int) age;-(void) SetName: (NSString *) name;-(NSString *) name; @end
#import "Person.h" @implementation person-(void) Setage: (int) age{ _age = age;} -(int) age{ return _age;} -(void) SetName: (NSString *) name{ _name = name;} -(NSString *) name{ return _name;} @end
@public: The member variables of an object can be accessed directly from anywhere
@private: can only be accessed directly in the object method of the current class ( @private is the default in the @implementation )
@protected: can be accessed directly in the object methods of the current class and its subclasses (the default is @protected in @interface )
@package: You can directly access the member variables of an object as long as you are in the same frame
member variables with the same name cannot be declared in @interface and @implementation
#import <foundation/foundation.h>int Main (int argc, const char * argv[]) { @autoreleasepool { //Insert C Ode here ... NSLog (@ "Hello, world!"); } return 0;} #import <Foundation/Foundation.h> @interface person:nsobject@end#import "Person.h" @implementation person@end
21-Dark Horse programmer------OC Language Learning Notes---point syntax and scope