Li Hongqiang iOS Development oc[018] The relationship between objects and methods

Source: Internet
Author: User

//

Main.m

- the relationship between objects and methods

//

Created by Vic fan on 16/7/14.

copyright©2016 year Li Hongqiang . All rights reserved.

//

/**

*

OC has a method of having a parameter return value

declaration with parameter return value method

-(int) sum: (int) x AndY: (int) y;

-(int) sum: (int) x AndY: (int) y{

return x+y;

relationship :

1 object as the parameter of the method ;

ways to display people's information

-(void) Showperson: (person *) person;

2 object as the return value of the method

denaturing Methods

-(Person *) Changesex: [person *] person;

*

*/

#import <Foundation/Foundation.h>

/**

* Declaration of Class

*/

/**

Define Gender enumeration

*/

typedef enum{

//0 1 2

Ksexwomen,ksexman,ksexyao

}sex;

@interface person : nsobject{

@public

nsstring *_name; name

int _age; Age

Sex _sex; sex

}

/**

* declaration of Methods

*/

-(void) Showperson: (person *) person;

-(person *) Changesex: [person *] person;

@end

@implementation Person

-(void) Showperson: (person *) person{

NSLog(@ " name :%@, age :%d, gender :%d",person->_name,person-> _age,person->_sex);

}

-(person *) Changesex: (person *) person{

person->_sex = ksexwomen;

return person ;

}

@end

int Main (int argc, const Char * argv[]) {

@autoreleasepool {

/**

* Create Objects

*/

person *p = [person new];

/**

* assigning value to an instance variable of an object

*/

p->_age = ;

p->_name = @ " Jay Chou ";

p->_sex = Ksexman;

/**

* Call Method

*/

[P Showperson:p];

NSLog(@ " sex :%d",p->_sex);

person *p2 = [P changesex:p];

NSLog(@ " sex :%d",p2->_sex);

}

return 0;

}

Li Hongqiang iOS Development oc[018] The relationship between objects and methods

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.