- #import <Foundation/Foundation.h>
- #pragma mark -
- @interface Person : NSObject
- {
- @public
- NSString *_name;
- NSString *_sex;
- }
- //对象作为方法的参数
- -(void)displayPerson:(Person *)person;
- //对象作为方法的返回值
- -(Person *)changePerson:(Person *)person;
- @end
- @implementation Person
- //返回值类型:void
- //参数是:Person类类型
- -(void)displayPerson:(Person *)person{
-
- NSLog(@"姓名=%@,性别=%@",person->_name,person->_sex);
- }
- /*
- 返回值类型:Person类类型
- 参数是:Person类类型
- */
- -(Person *)changePerson:(Person *)person{
-
- person->_name = @"叶良辰";
- person->_sex = @"男";
-
-
- return person;
- }
- @end
- int main(int argc, const char * argv[]) {
- @autoreleasepool {
-
- Person *p = [Person new];
- Person *p1=[Person new];
-
- p1->_name = @"猪八戒";
- p1->_sex = @"男";
- //对象作为方法的参数使用
- [p displayPerson:p1];
-
- //定义一个perTemp 接收 方法返回的那个对象
- Person *perTemp = [p changePerson:p1]; //有返回值,返回的是p1的地址
- [p displayPerson:perTemp];
-
-
-
- }
- return 0;
- }
复制代码
|
|