/*
枪类
名称 Gun
属性 枪的型号(_size)
行为 射击(shoot)
士兵类
名称 Soldier
属性 姓名(_name) 生命(_life) 等级(_level)
行为 开枪(shootByGun)
子弹类
名称 Bullet
属性 子弹个数(_bulletCount) 型号(_model)
*/
#import <Foundation/Foundation.h>
#pragma mark 子弹类的声明
@interface Bullet:NSObject
{
@public
int _bulletCount;
NSString *_model;
}
@end
#pragma mark 子弹类的实现
@implementation Bullet
@end
#pragma mark 枪类的声明
@interface Gun:NSObject
{
@public
NSString *_size;
}
-(void)shoot:(Bullet *) bullet;
@end
#pragma mark 枪类的实现
@implementation Gun
-(void)shoot:(Bullet *) bullet{
if (bullet->_bulletCount> 0) {
bullet->_bulletCount --;
NSLog(@"枪正在射击,子弹还有%d发",bullet->_bulletCount);
}else{
NSLog(@"没有子弹了!");
}
}
@end
#pragma mark 士兵类的声明
@interface Soldier:NSObject
{
@public
NSString *_name;
int _level;
int _life;
}
-(void)shootByGun:(Gun *)gun andBullet:(Bullet *) bullet;
@end
#pragma mark 士兵类的实现
@implementation Soldier
-(void)shootByGun:(Gun *)gun andBullet:(Bullet *) bullet{
[gun shoot:bullet];
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
//创建子弹
Bullet *bulletOne = [Bullet new];
bulletOne->_bulletCount = 1;
//bulletOne->_model = @"九五式手枪的子弹";
//创建枪
Gun *gunOne = [Gun new];
//gunOne->_size = @"九五式手枪";
//创建士兵
Soldier *soldierOne = [Soldier new];
// soldierOne->_name = @"张三丰";
// soldierOne->_life = 100;
// soldierOne->_level =2;
[soldierOne shootByGun:gunOne andBullet:bulletOne];
[soldierOne shootByGun:gunOne andBullet:bulletOne];
[soldierOne shootByGun:gunOne andBullet:bulletOne];
[soldierOne shootByGun:gunOne andBullet:bulletOne];
NSLog(@"%d",bulletOne->_bulletCount);
;
}
return 0;
}
|
|