IOS學習筆記十三(使用類別實現(xiàn)非正式協(xié)議)

1、協(xié)議

可以理解為java里面的接口

 
2、測試Demo

NSObject+chenyu.h

    #ifndef NSObject_chenyu_h
    #define NSObject_chenyu_h
    #import <Foundation/Foundation.h>
     
    @interface NSObject(chenyu)
    -(void)test;
    @end
     
    #endif /* NSObject_chenyu_h */

 

Org.h

    #ifndef Org_h
     
    #define Org_h
     
    #import <Foundation/Foundation.h>
     
    #import "NSObject+chenyu.h"
     
    @interface Org : NSObject
     
    @end
     
     
     
    #endif /* Org_h */



Org.m

    #import <Foundation/Foundation.h>
    #import "Org.h"
    //#import "NSObject+chenyu.h"
     
    @implementation Org
    //如果不實現(xiàn)協(xié)議中的方法會發(fā)生錯誤
    -(void)test
    {
        NSLog(@"this is org test method");
    }
    @end


 

main.m

    #import "Org.h"
     
    int main(int argc, char * argv[]) {
        @autoreleasepool {
            Org *org = [Org new];
            [org test];
        }
    }



3、運行結(jié)果

this is org test method




 

 

 
 
作者:chen.yu
深信服三年半工作經(jīng)驗,目前就職游戲廠商,希望能和大家交流和學習,
微信公眾號:編程入門到禿頭 或掃描下面二維碼
零基礎(chǔ)入門進階人工智能(鏈接)