黑马程序员技术交流社区

标题: 士兵 [打印本页]

作者: muyan091115    时间: 2016-5-26 23:26
标题: 士兵
main.n
  1. #import <Foundation/Foundation.h>
  2. #import "Soldier.h"

  3. int main(int argc, const char * argv[]) {
  4.     @autoreleasepool {
  5.         
  6.         Bullet * bullet = [Bullet new];
  7.         [bullet setMaxCapcity :100];
  8.         [bullet setBulletCount :3];
  9.         Gun *AK47 = [Gun new];
  10.         [AK47 setModel: @"牛B"];
  11.         [AK47 setBullet :bullet];
  12.         Soldier *JW = [Soldier new];
  13.         [JW setName :@"蒋伟"];
  14.         [JW setType :@"特种兵" ];
  15.         [JW setGun :AK47];
  16.         
  17.         [JW fire];
  18.         [JW fire];
  19.         [JW fire];
  20.         [JW fire];
  21.         [JW fire];
  22.         [JW fire];
  23.         
  24.     }
  25.     return 0;
  26. }
复制代码

Gun.h
  1. #import <Foundation/Foundation.h>
  2. #import "Bullet.h"

  3. @interface Gun : NSObject{
  4.     NSString *_model;
  5.     int _sheCheng;
  6.     Bullet *_bullet;
  7. }
  8. - (void) setModel :(NSString *)model;
  9. - (NSString *)model;

  10. - (void)setSheCheng :(int)sheCheng;
  11. - (int)sheCheng;

  12. - (void)setBullet :(Bullet *)bullet;
  13. - (Bullet *)bullet;

  14. - (void)shoot;

  15. @end
复制代码

Gun.m
  1. #import "Gun.h"

  2. @implementation Gun
  3. - (void) setModel :(NSString *)model{
  4.     _model = model;
  5. }
  6. - (NSString *)model{
  7.     return _model;
  8. }

  9. - (void)setSheCheng :(int)sheCheng{
  10.     _sheCheng = sheCheng;
  11. }
  12. - (int)sheCheng{
  13.     return _sheCheng;
  14. }

  15. - (void)setBullet :(Bullet *)bullet{
  16.     _bullet = bullet;
  17. }
  18. - (Bullet *)bullet{
  19.     return _bullet;
  20. }

  21. - (void)shoot{
  22.     if([_bullet bulletCount] <= 0){
  23.         NSLog(@"\n没有子弹了!");
  24.     }else{
  25.         NSLog(@"\n兔兔兔!");
  26.         int num = [_bullet bulletCount];
  27.         [_bullet setBulletCount :(num-1)];
  28.         NSLog(@"\n剩余子弹数:%d",num-1);
  29.     }
  30. }
  31. @end
复制代码

Soldier.h
  1. #import <Foundation/Foundation.h>
  2. #import "Gun.h"

  3. @interface Soldier : NSObject{
  4.     NSString *_name;
  5.     NSString *_type;
  6.     Gun * _gun;
  7. }

  8. - (void)setName :(NSString *)name;
  9. - (NSString *)name;

  10. - (void)setType :(NSString *)type;
  11. - (NSString *)type;

  12. - (void)setGun :(Gun *)gun;
  13. - (Gun *)gun;

  14. - (void)fire;

  15. @end
复制代码

Soldier.m
  1. #import "Soldier.h"

  2. @implementation Soldier
  3. - (void)setName :(NSString *)name{
  4.     _name = name;
  5. }
  6. - (NSString *)name{
  7.     return _name;
  8. }

  9. - (void)setType :(NSString *)type{
  10.     _type = type;
  11. }
  12. - (NSString *)type{
  13.     return _type;
  14. }

  15. - (void)setGun :(Gun *)gun{
  16.     _gun = gun;
  17. }
  18. - (Gun *)gun{
  19.     return _gun;
  20. }

  21. - (void)fire{
  22.     NSLog(@"\n预备开火!");
  23.     [_gun shoot];
  24. }

  25. @end
复制代码

Bullet.h
  1. #import <Foundation/Foundation.h>

  2. @interface Bullet : NSObject{
  3.     int _maxCapcity;
  4.     int _bulletCount;
  5. }

  6. - (void)setMaxCapcity :(int)maxCapcity;
  7. - (int)maxCapcity;

  8. - (void)setBulletCount :(int)bulletCount;
  9. - (int)bulletCount;

  10. @end
复制代码

Bullet.m
  1. #import "Bullet.h"

  2. @implementation Bullet

  3. - (void)setMaxCapcity :(int)maxCapcity{
  4.     _maxCapcity = maxCapcity;
  5. }
  6. - (int)maxCapcity{
  7.     return _maxCapcity;
  8. }

  9. - (void)setBulletCount :(int)bulletCount{
  10.     _bulletCount = bulletCount;
  11. }
  12. - (int)bulletCount{
  13.     return _bulletCount;
  14. }

  15. @end
复制代码




作者: qq874983484    时间: 2016-5-28 01:05
看着好厉害呢。
作者: muyan091115    时间: 2016-5-28 23:32
没啥 就是写简单的课后作业 今天学了@property才知道写的都是废代码   555




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2