黑马程序员技术交流社区
标题:
士兵
[打印本页]
作者:
muyan091115
时间:
2016-5-26 23:26
标题:
士兵
main.n
#import <Foundation/Foundation.h>
#import "Soldier.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Bullet * bullet = [Bullet new];
[bullet setMaxCapcity :100];
[bullet setBulletCount :3];
Gun *AK47 = [Gun new];
[AK47 setModel: @"牛B"];
[AK47 setBullet :bullet];
Soldier *JW = [Soldier new];
[JW setName :@"蒋伟"];
[JW setType :@"特种兵" ];
[JW setGun :AK47];
[JW fire];
[JW fire];
[JW fire];
[JW fire];
[JW fire];
[JW fire];
}
return 0;
}
复制代码
Gun.h
#import <Foundation/Foundation.h>
#import "Bullet.h"
@interface Gun : NSObject{
NSString *_model;
int _sheCheng;
Bullet *_bullet;
}
- (void) setModel :(NSString *)model;
- (NSString *)model;
- (void)setSheCheng :(int)sheCheng;
- (int)sheCheng;
- (void)setBullet :(Bullet *)bullet;
- (Bullet *)bullet;
- (void)shoot;
@end
复制代码
Gun.m
#import "Gun.h"
@implementation Gun
- (void) setModel :(NSString *)model{
_model = model;
}
- (NSString *)model{
return _model;
}
- (void)setSheCheng :(int)sheCheng{
_sheCheng = sheCheng;
}
- (int)sheCheng{
return _sheCheng;
}
- (void)setBullet :(Bullet *)bullet{
_bullet = bullet;
}
- (Bullet *)bullet{
return _bullet;
}
- (void)shoot{
if([_bullet bulletCount] <= 0){
NSLog(@"\n没有子弹了!");
}else{
NSLog(@"\n兔兔兔!");
int num = [_bullet bulletCount];
[_bullet setBulletCount :(num-1)];
NSLog(@"\n剩余子弹数:%d",num-1);
}
}
@end
复制代码
Soldier.h
#import <Foundation/Foundation.h>
#import "Gun.h"
@interface Soldier : NSObject{
NSString *_name;
NSString *_type;
Gun * _gun;
}
- (void)setName :(NSString *)name;
- (NSString *)name;
- (void)setType :(NSString *)type;
- (NSString *)type;
- (void)setGun :(Gun *)gun;
- (Gun *)gun;
- (void)fire;
@end
复制代码
Soldier.m
#import "Soldier.h"
@implementation Soldier
- (void)setName :(NSString *)name{
_name = name;
}
- (NSString *)name{
return _name;
}
- (void)setType :(NSString *)type{
_type = type;
}
- (NSString *)type{
return _type;
}
- (void)setGun :(Gun *)gun{
_gun = gun;
}
- (Gun *)gun{
return _gun;
}
- (void)fire{
NSLog(@"\n预备开火!");
[_gun shoot];
}
@end
复制代码
Bullet.h
#import <Foundation/Foundation.h>
@interface Bullet : NSObject{
int _maxCapcity;
int _bulletCount;
}
- (void)setMaxCapcity :(int)maxCapcity;
- (int)maxCapcity;
- (void)setBulletCount :(int)bulletCount;
- (int)bulletCount;
@end
复制代码
Bullet.m
#import "Bullet.h"
@implementation Bullet
- (void)setMaxCapcity :(int)maxCapcity{
_maxCapcity = maxCapcity;
}
- (int)maxCapcity{
return _maxCapcity;
}
- (void)setBulletCount :(int)bulletCount{
_bulletCount = bulletCount;
}
- (int)bulletCount{
return _bulletCount;
}
@end
复制代码
作者:
qq874983484
时间:
2016-5-28 01:05
看着好厉害呢。
作者:
muyan091115
时间:
2016-5-28 23:32
没啥 就是写简单的课后作业 今天学了@property才知道写的都是废代码 555
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2