//
// main.m
// 07.士兵突击
/// <#Description#>
// Created by mac on 15/10/8.
// Copyright © 2015年 mac-wangwenfeng. All rights reserved.
//
#import <Foundation/Foundation.h>
#pragma mark 子弹类创建
@interface Bullet : NSObject{
@public
NSString *_name;
//int _num;
}
@end
#pragma mark 子弹类实现
@implementation Bullet
@end
#pragma mark 枪类创建
@interface Gun:NSObject{
@public
NSString *_size;
int _num;
}
-(void)gunShoot:(Bullet *) danjia;
//-(void)exchange:(Bullet *) danJia;
@end
#pragma mark 枪类实现
@implementation Gun
-(void)gunShoot:(Bullet *) danjia{
_num--;
if(_num>=0)
NSLog(@"\n塔塔塔...枪在射击%@...剩余子弹数%d",_size,_num);//如果我想再一个方法里面使用,另一个类的成员变量,怎么弄
else
NSLog(@"\n没子弹了还射个球!");
}
//-(void)exchange:(Bullet *) danJia
//{
// if (_num<=0) {
// NSLog(@"子弹数不足,请更换弹夹!!!");
// }
// // if(_num<=0)
//// goto start;
//}
@end
#pragma mark 士兵类创建
@interface Soldier:NSObject{
@public
NSString *_name;
int life;
int level;
}
-(void)gunFire:(Gun *) gun and:(Bullet *) bullet;
@end
#pragma mark 士兵类实现
@implementation Soldier
-(void)gunFire:(Gun *) gun and:(Bullet *) bullet{
[gun gunShoot:bullet];
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Soldier *p=[Soldier new];
p->_name=@"大兵瑞恩";
p->level=5;
p->life=99;
Gun *gun=[Gun new];
gun->_size=@"ak47";
gun->_num=4;
Bullet *bullet=[Bullet new];
//bullet->_num=4;
bullet->_name=@"1.1口径";
[p gunFire: gun and:bullet];
[p gunFire: gun and:bullet];
[p gunFire: gun and:bullet];
[p gunFire: gun and:bullet];
[p gunFire: gun and:bullet];
}
return 0;
}
如上面的代码,打印结果如下
2015-10-08 23:48:23.041 07.士兵突击[1142:42240]
塔塔塔...枪在射击ak47...剩余子弹数3
2015-10-08 23:48:23.043 07.士兵突击[1142:42240]
塔塔塔...枪在射击ak47...剩余子弹数2
2015-10-08 23:48:23.043 07.士兵突击[1142:42240]
塔塔塔...枪在射击ak47...剩余子弹数1
2015-10-08 23:48:23.043 07.士兵突击[1142:42240]
塔塔塔...枪在射击ak47...剩余子弹数0
2015-10-08 23:48:23.043 07.士兵突击[1142:42240]
没子弹了还射个球!
Program ended with exit code: 0
其实我想要的结果是枪在射击1.1口径的子弹,能实现一个类的方法调用另一个类的实例变量么? |
|