OC Language Learning (c) member variable Get/set method and "." Syntax, @proterty, and @synthesize keywords

Source: Internet
Author: User

Person.h

#ifndef oc_person_h#define oc_person_h@interface person:nsobject{    int age; @protected    float height;} -(int) age; Get Method-(void) Setage: (int) pAge;  Set Method @end#endif

Person.m

#import <Foundation/Foundation.h> #import "Person.h" @implementation person-(int) age{    return age;} -(void) Setage: (int) page{Age    = PAge;} @end

main.m

int main () {    person* per = [[Person alloc] init];    int age = [per age]; Call the Get method    [per setage:16];//Call the Set method        //use "." To invoke Get/set  using the original variable name, which requires the variable's get, set all conform to the convention    int age2 = Per.age; Get    per.age = n;//set        return 0;}

every time this writing Get/set method, very troublesome, OC has an automated method that uses@protertyand the@synthesizeKey Words

Person.h

#ifndef oc_person_h#define oc_person_h@interface person:nsobject{    int age; @protected    float height;} -(int) age;//-(void) Setage: (int) pAge; @property int age; The compiler automatically interprets the declaration of the Get/set method into int age @end#endif

person.m

#import <Foundation/Foundation.h> #import "Person.h" @implementation person//-(int) age//{//    return age;//} -(void) Setage: (int) page//{//Age    = page;//} @synthesize age;//compiler automatically interprets get, set method implementations of age @end





OC Language Learning (c) member variable Get/set method and "." Syntax, @proterty, and @synthesize keywords

Related Article

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.