黑马程序员技术交流社区
标题:
对象作为方法的参数的连续传递练习
[打印本页]
作者:
WinnieY
时间:
2015-10-11 21:10
标题:
对象作为方法的参数的连续传递练习
#import <Foundation/Foundation.h>
#pragma mark - 子弹
@interface Bullet:NSObject
{
@public
NSString *_model;
int _bulletnum;
}
@end
@implementation Bullet
@end
#pragma mark - 枪
@interface Gun :NSObject
{
@public
NSString *_size;
}
-(void)shoot:(Bullet *)bulletnum;
@end
@implementation Gun
-(void)shoot:(Bullet *)bulletnum
{
if(bulletnum->_bulletnum>0)
{
bulletnum->_bulletnum--;
NSLog(@"子弹剩余数量是%d",bulletnum->_bulletnum);
}
else
{
NSLog(@"没有子弹了");
}
}
@end
#pragma mark - 人
@interface Person : NSObject
{
NSString *_name;
}
-(void)kaiqiang:(Gun *)gun and:(Bullet *)bullet;
@end
@implementation Person
-(void)kaiqiang:(Gun *)gun and:(Bullet *)bullet
{
[gun shoot:bullet];
NSLog(@"队长别开枪");
}
@end
int main()
{
Gun *ak=[Gun new];
Bullet *bullet=[Bullet new];
bullet->_bulletnum=2;
Person *wangcai=[Person new];
[wangcai kaiqiang:ak and:bullet];
[wangcai kaiqiang:ak and:bullet];
[wangcai kaiqiang:ak and:bullet];
[wangcai kaiqiang:ak and:bullet];
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2