1. #import <Foundation/Foundation.h> 2. #pragma mark 子弹类的声明 3. @interface Bullet:NSObject 4. { 5. @public 6. NSString *_model; 7. int _bulletCount; 8. } 9. @end 10. #pragma mark 子弹类的实现 11. @implementation Bullet 12. @end 13. #pragma mark 枪类的声明 14. @interface Gun:NSObject 15. { 16. @public 17. NSString *_size; 18. } 19. -(void)shoot:(Bullet *) bullet; 20. @end 21. #pragma mark 枪类的实现 22. @implementation Gun 23. -(void)shoot:(Bullet *)bullet{ 24. if (bullet->_bulletCount>0) { 25. bullet->_bulletCount--; 26. NSLog(@"枪在射击,剩余子弹%d",bullet->_bulletCount); 27. }else{ 28. NSLog(@"没子弹了"); 29. } 30. } 31. @end 32. #pragma mark 人类的声明 33. @interface Person:NSObject 34. { 35. @public 36. NSString *_name; 37. int life; 38. int level; 39. } 40. -(void)fire:(Gun *)gun andBullet:(Bullet *)bullet; 41. @end 42. #pragma mark 人类的实现 43. @implementation Person 44. -(void)fire:(Gun *)gun andBullet:(Bullet *)bullet{ 45. [gun shoot:bullet]; 46. } 47. @end 48. int main(int argc,const char * argv[]){ 49. @autoreleasepool { 50. Bullet *bullet=[Bullet new]; 51. bullet->_model=@"5mm口径"; 52. bullet->_bulletCount=3; 53. 54. Gun *gun=[Gun new]; 55. gun->_size=@"步枪"; 56. 57. Person *p=[Person new]; 58. p->_name=@"三朵"; 59. p->life=100; 60. p->level=2; 61. 62. [p fire:gun andBullet:bullet]; 63. [p fire:gun andBullet:bullet]; 64. [p fire:gun andBullet:bullet]; 65. [p fire:gun andBullet:bullet]; 66. 67. } 68. } |
|