0
点赞
收藏
分享

微信扫一扫

OC 对象作为方法的参数连续传递


OC 对象作为方法的参数连续传递_对象

实现1:

#import <Foundation/Foundation.h>
@interface Gun:NSObject{
@public
NSString *_type;
int _bllutCount;
}
-(void)shot;
@end
@implementation Gun
-(void)shot{
if(_bllutCount >0){
_bllutCount--;
}

NSLog(@"%@ is shoting ..., the bllut is leave :%d",_type,_bllutCount);
}


@end

@interface Person : NSObject{
@public
NSString *_name;
int _life;
int _level;
}
-(void)fire:(Gun *) gun;

@end

@implementation Person
-(void)fire:(Gun *) gun{
[gun shot];
}


@end

int main(int argc, const char * argv[])
{

@autoreleasepool {

//
Person *p = [Person new];
p->_name = @"tom";
p->_life = 100;
p->_level = 10;

Gun *gun = [Gun new];
gun ->_type = @"china";
gun ->_bllutCount = 6;

[p fire:gun];
[p fire:gun];
[p fire:gun];

NSLog(@"Hello, World!");

}
return 0;
}

实现2:


举报

相关推荐

0 条评论