#import <Foundation/Foundation.h>
#pragma mark 子弹类
@interface Bullet:NSObject
{
@public
NSString *_size;//子弹型号
int _bulletCount;//子弹数
}
@end
@implementation Bullet
@end
#pragma mark 枪类
@interface Gun : NSObject
{
@public
NSString *_size;//型号
}
-(void)shoot:(Bullet *)bullet;//射击动作
@end
@implementation Gun
-(void)shoot:(Bullet *)bullet{
if (bullet->_bulletCount>0) {
bullet->_bulletCount -= 1;
NSLog(@"%@正在突突的射击。。。.剩余子弹%d",_size,bullet->_bulletCount);
}else{
NSLog(@"没有子弹,请换子弹");
}
}
@end
#pragma mark 士兵类
@interface Soldier : NSObject
{
@public
NSString *_name;
int life;
int level;
}
-(void)fireByGun:(Gun *)gun and: (Bullet *)bullet;
@end
@implementation Soldier
-(void)fireByGun:(Gun *)gun and: (Bullet *)bullet{
[gun shoot:bullet];
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Soldier *s = [Soldier new];
s->_name=@"三朵";
s->life=98;
s->level=2;
Gun *gun = [Gun new];
gun->_size=@"小米加步枪";
Bullet *bullet = [Bullet new];
bullet->_bulletCount=3;
[s fireByGun:gun and:bullet];
[s fireByGun:gun and:bullet];
[s fireByGun:gun and:bullet];
[s fireByGun:gun and:bullet];
}
return 0;
} |
|