21-Dark Horse programmer------OC Language Learning Notes---point syntax and scope

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.