美文网首页八天学会OC
第02天OC语言(07):#pragmamark基本使用

第02天OC语言(07):#pragmamark基本使用

作者: liyuhong | 来源:发表于2017-07-13 11:02 被阅读15次
    一、概念
    二、代码
    #import <Foundation/Foundation.h>
    #pragma mark 类
    #pragma mark - 2.枪
    
    @interface Gun : NSObject
    {
    @public
        int _bullet; // 子弹
    }
    
    - (void)shoot;
    @end
    
    @implementation Gun
    
    - (void)shoot
    {
        if (_bullet > 0)
        {
            _bullet--;
            NSLog(@"打了一枪 %i",_bullet);
        }
        else
        {
            NSLog(@"没有子弹了,请换弹夹");
        }
        
    }
    
    @end
    #pragma mark - 1.士兵
    
    @interface Soldier : NSObject
    {
    @public
        NSString *_name;
        double _height;
        double _weight;
    }
    - (void)fire:(Gun *)gun;
    @end
    
    @implementation Soldier
    
    - (void)fire:(Gun *)g
    {
        [g shoot];
    }
    
    @end
    
    
    
    #pragma mark - 3.弹夹
    #pragma mark - main函数
    int main(int argc, const char * argv[])
    {
        Soldier *s = [Soldier new];
        s->_name = @"lyh";
        s->_height = 1.71;
        s->_weight = 65.0;
        
        Gun *gp = [Gun new];
        gp->_bullet = 10;
        
        [s fire:gp]; // 地址
        [s fire:gp];
        [s fire:gp];
        [s fire:gp];
        [s fire:gp];
        [s fire:gp];
        
        return 0;
    }
    
    image.png

    相关文章

      网友评论

        本文标题:第02天OC语言(07):#pragmamark基本使用

        本文链接:https://www.haomeiwen.com/subject/nicphxtx.html