1.找对象 (抽象对象) 有哪些对象是必要的 士兵: 类名:Soldier 属性:life name level 功能:跑 跳 打手枪 开坦克 蹲下 瞄准 换枪 换子弹 枪: 属性:型号,颜色,子弹 功能: 打子弹 Tank: 2.设计类 类名: 属性: 功能: 3.代码实现 // 用面向对象模拟士兵开枪场景:注意体会多个对象之间的组合关系、对象作为函数参数传递、对象的连续传递 #import #pragma mark - 子弹类 @interface Bullet : NSObject { } @end @implementation Bullet @end #pragma mark - 枪 @interface Gun : NSObject { } // 射击方法 - (void)shoot; - (void)shootWithBullet:(Bullet *)bullet; @end @implementation Gun // 如果项目变动过大,不建议修改以前的代码,而是新增方法 - (void)shoot { // // } - (void)shootWithBullet:(Bullet *)bullet { // } @end typedef enum { }SoldierLevel; #pragma mark - 士兵类 @interface Soldier : NSObject { } - (void)fireByGun:(Gun *)gun; - (void)fireByGun:(Gun *)gun andBullet: (Bullet *)bullet; @end @implementation Soldier - (void)fireByGun:(Gun *)gun { // // } - (void)fireByGun:(Gun *)gun andBullet:(Bullet *)bullet { } @end #pragma mark - 主函数 int main(int argc, const char * argv[]) { // // } void test() { } |
多文件实现
// #import #import "Gun.h" #import "Iphone.h" typedef enum { }SoldierLevel; @interface Soldier : NSObject { @public } - (void)fireByGun:(Gun *)gun; - (void)fireByGun:(Gun *)gun andBullet: (Bullet *)bullet; - (void)call:(Iphone *)phone; @end // #import "Soldier.h" // contrl + command + ↑ @implementation Soldier - (void)fireByGun:(Gun *)gun { } - (void)fireByGun:(Gun *)gun andBullet:(Bullet *)bullet { } - (void)call:(Iphone *)phone { } @end // #import #import "Bullet.h"//.h @interface Gun : NSObject { @public } // 射击方法 - (void)shoot; - (void)shootWithBullet:(Bullet *)bullet; @end // #import "Gun.h" @implementation Gun // 如果项目变动过大,不建议修改以前的代码,而是新增方法 - (void)shoot { } - (void)shootWithBullet:(Bullet *)bullet { } @end // #import #import "Soldier.h" int main(int argc, const char * argv[]) { // // } |