zoukankan      html  css  js  c++  java
  • 对象作为参数传递

    //  对象作为参数传递
    //好好感受一下面向对象的思想,枪有射击的功能,士兵使用枪射击,就给士兵一个枪,然后调用枪的射击方法就可以了
    
    #import <Foundation/Foundation.h>
    
    #pragma mark - 枪
    ////事物名称:枪(Gun)
    //属性:弹夹(clip) , 型号(model)
    //行为:射击
    @interface Gun : NSObject
    {
        @public
        int _bullet; // 子弹数
        int _model;
    }
    - (void)shoot; // 射击
    @end
    
    @implementation Gun
    - (void)shoot{
        if (_bullet>0) {
            NSLog(@"射击了一次,还剩%i个子弹",--_bullet);
        }
    }
    @end
    
    #pragma mark - 士兵
    //士兵
    //事物名称: 士兵(Soldier)
    //属性:姓名(name), 身高(height), 体重(weight)
    //行为:打枪(fire), 打电话(callPhone)
    @interface Soldier : NSObject
    {
        @public
        NSString *_name;
        float _height;
        float _weight;
    }
    - (void)fire:(Gun*)mGun;
    - (void)call;
    @end
    
    @implementation Soldier
    
    -(void)fire:(Gun *)mGun{
        [mGun shoot];
    }
    
    -(void)call{
        NSLog(@"士兵在打电话");
    }
    @end
    
    int main(int argc, const char * argv[]) {
    //    创建士兵对象
        Soldier *ps = [Soldier new];
    //    初始化士兵对象
        ps->_name=@"瑞恩";
        ps->_height=1.88;
        ps->_weight=100;
        
    //    创建枪对象
        Gun *pg=[Gun new];
    //    初始化枪对象
        pg->_bullet=10;
        pg->_model=47;
        
    //    调用射击方法
        [ps fire:pg];
        [ps fire:pg];
        [ps fire:pg];
        [ps fire:pg];
        [ps fire:pg];
        
        return 0;
    }
  • 相关阅读:
    Android 权限表
    自己动手写shell命令之write
    libgdx 1.4.1公布
    【JUnit4.10源码分析】5.2 Rule
    TCP协议具体解释(上)
    关于 二维码 与 NFC 之间的出身贫贱说
    Effective C++ Item 42 了解 typename 的双重意义
    C++第12周(春)项目2
    HDU 2256 Problem of Precision(矩阵高速幂)
    【OC语法快览】二、存取方法
  • 原文地址:https://www.cnblogs.com/imChay/p/5590229.html
Copyright © 2011-2022 走看看