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;
    }
  • 相关阅读:
    linux---shell数组
    linux---shell传递参数
    Windows mysql免安装版配置。(版本号-5.6.45);
    contos 6.9 和 centos7 配置docker?
    数据库表的演化过程
    docker简单介绍。
    Linux 配置 mycat 和 分库分表配置。
    Linux 配置mysql 免安装版。
    mycat 简单介绍
    kibana 对es的简单操作。
  • 原文地址:https://www.cnblogs.com/imChay/p/5590229.html
Copyright © 2011-2022 走看看