士兵开枪,枪发射子弹 的一个实现
#import <Foundation/Foundation.h>
@interface Bullet: NSObject
{
@public
NSString *_size;
int _bulletcount;
}
@end
@implementation Bullet
@end
@interface Gun : NSObject
{
@public
NSString *_size;
}
-(void)shoot:(Bullet *) bullet;
@end
@implementation Gun
-(void)shoot:(Bullet *) bullet{
if (bullet->_bulletcount>0){
bullet->_bulletcount--;
NSLog(@"%@正在射击,剩余子弹%d",_size,bullet->_bulletcount);
}else{
NSLog(@"没子弹了,请添加子弹");
}
}
@end
@interface Solider : NSObject
{
@public
NSString *_name;
int _life;
int _level;
}
-(void)fire:(Gun *)gun andBullet:(Bullet *) bullet;
@end
@implementation Solider
-(void)fire:(Gun *)gun andBullet:(Bullet *) bullet{
[gun shoot:bullet];
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Solider *s=[Solider new];
s->_name=@"kaiyi";
s->_level=80;
s->_life=5000;
Gun *gun=[Gun new];
gun->_size=@"机关枪";
Bullet *bullet=[Bullet new];
bullet->_size=@"A1";
bullet->_bulletcount=2;
[s fire:gun andBullet:bullet];
[s fire:gun andBullet:bullet];
[s fire:gun andBullet:bullet];
}
return 0;
}
|