OC Set/get method

Source: Internet
Author: User

Create a new "Cocoa Touch Class" file named people

People.h Write

@interface People:nsobject@property    int Age ; @end

PEOPLE.M write (in fact automatically generated)

@implementation people @end

Use occasions:

intMainintargcChar*argv[]) {People*p=[[People alloc]init]; P.age=Ten;//using the Set method directlyNSLog (@"Age %d\n", p.age);//using the Get method directly[P Setage: -];//calling the Set methodNSLog (@"Age %d\n", [P age]);//call the Get method    inttest; Test=1; NSLog (@"Age %d\n", test); }

Manual Write Set/get method

The Set/get method can be generated automatically by @property in the example above, and we can also write the Set/get method manually.

Modify People.h

@interface people:nsobject{    int  _age;} @property   int age ; @end

Modify PEOPLE.M

@implementation People-(void) Setage: (int) age{    NSLog (@ "setage" );    _age= Age;} -(int) age{    NSLog (@ "getAge");     return _age;} @end

Through the personality, and then execute the program, found that we manually write the Set/get method of the output of the log.

OC Set/get method

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.