Dark Horse programmer------OC Point syntax, member variable scope, @property, and @synthesize

Source: Internet
Author: User
Tags variable scope

-----iOS training, Android training, Java training, and look forward to communicating with you----- OC point syntax, member variable scope, @property, and @synthesize
One, dot syntax1) basic concept of point syntax : When we use object-oriented encapsulation features, we privatize member variables, take the use of setter methods and getter methods to provide member variable access scheme outside. Then we know that the way OC calls methods is very unique, using the [object name (class name) method name] , then a lot of Java, C # Programmers are not used to, in Java, C # These mainstream object-oriented language, the way to invoke the method is to use  Object name (class name). The method name ()can also direct the object name if the member variable takes public access . member variable = value; the form. It may be that Java, C # programmers quickly get used to the syntax of OC, Apple let OC birth point syntax. 2) Point Syntax Code First experience
First, we declare a person class
Person.h
  person.h//  04-point syntax//#import <Foundation/Foundation.h> @interface person:nsobject{    int _age ;    NSString *_name;} -(void) Setage: (int) age;-(int) age;-(void) SetName: (NSString *) name;-(NSString *) name; @end


Person.m
#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

Test procedure:
#import <Foundation/Foundation.h> #import "Person.h" int main (int argc, const char * argv[]) {person    *p = [Perso n new];        The essence of point syntax is the method call    P.age = ten;//[P setage:10];        int a = P.age; [P age];            P.name = @ "Jack";        NSString *s = p.name;        NSLog (@ "%@", s);        return 0;}



Note the point:1) The essence of point syntax is to invoke the getter method of the class and setter method, if there is no getter method and setter method in the class can not use point syntax. Point grammatical nature:
The essence of point syntax is the method call    P.age = ten;//[P setage:10];        int a = P.age; [P age];

2) The following improper call can lead to a dead loop
Will cause a dead loop    //self.age = age;//equivalent to calling [self setage:age];}

Will cause a dead loop    //return self.age;//equivalent to [self-age];




second, member variable scope
@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 (default is @private in @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

 
@public//Direct access to the member variable int _age of the object anywhere    ;            @private  //The int _height can only be accessed directly in the object method of the current class    ;        @protected//can access int _weight directly in the object method of the current class and subclass    ;    int _money;


Note:1)member variables with the same name cannot be declared in the @interface and @implementation 2) subclass inherits parent class, subclass has @public and @protected decorated member variables of parent class 3) If you declare a property in a class and do not specify access rights, the default is @protected

iii. @property and @synthesize
These two keywords appear to eliminate setter methods and getter methods in the code
1) @property
@property: You can automatically generate a setter and Getter declaration for a member variable @property int age;//equivalent to the following two sentences:-(void) Setage: (int) age;-(int) age;
2) @synthesize
The @synthesize automatically generates the setter and getter implementations of age, and accesses the _age member variable @synthesize-= _age; @synthesize height = _height; @synthesize Weight = _weight, name = _name;


In this way, we omit the time to write setter methods and getter methods, but it is also extremely boring and meaningless to write.
However, even if this is still very uncomfortable, in the old version of Xcode, in order to avoid writing setter method and getter method, we must also write @property int _age between @interface and @end; Write @synthesize code between @implementation and @end, and fortunately, you are living in a happy time, since xcode4.4, @property int _age; This code put @property and @synthesize work to dry. It mainly does the following things: 1) Generate a declaration of the Get and set methods of the _age member variable; 2) generate the _age member variable set and get method implementation; 3) generate a _age member variable.

of course, the getter method and setter method generated by the @property method may not meet your needs, and you can manually add getter methods and Setter methods yourself ,If you manually write the setter method @property will not generate setter method, but will still generate getter method, getter Similarly, if you will setter method, getter method is written manually, @ Property will not generate the appropriate getter method and setter method for you.

Dark Horse programmer------OC Point syntax, member variable scope, @property, and @synthesize

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.