• [Objective-c 基础


    A.概念

    1.用来声明方法(不能声明成员变量)
    2.只要某个类遵守了这个协议,相当于拥有了协议中得所有方法的声明
    3.属性
    (1)@required:默认,要求实现,不实现就会发出警告
    (2)@optional:不要求实现
    ————MyProtocol.h--------------
     1 @protocol MyProtocol
     2 @required
     3 - (void) test1;
     4 
     5 @optional
     6 - (void) test2;
     7 
     8 - (void) test3;
     9 
    10 @end
     
    4.父类遵循的协议,子类也会继承
     
    5.基协议
    (1)一个协议遵守了另外一个协议,就可以拥有其所有声明的方法
    ————MyProtocol.h———————
    1 #import <Foundation/Foundation.h>
    2 #import "MyProtocol.h"
    3 
    4 @protocol MyProtocol2 <MyProtocol>
    5 
    6 
    7 @end
     
    (2)基协议<NSObject>
    a..建议所有协议都遵循基协议
    @protocol MyProtocol <NSObject>
     
    b.动态遵守协议
        Person<MyProtocol2> *p = [[Person alloc] init];
     
    c.遵守多个协议
    @interface Person : NSObject <MyProtocol, MyProtocol2>
     
     
    6.协议和成员对象
    ===========Person.h====================
    1 @interface Person : NSObject <MyProtocol]] >
    2 
    3 @property(nonatomic, strong) id<MyProtocol2> obj;
    4 
    5 @end
     
    ===========main.c======================
    1 int main(int argc, const char * argv[]) {
    2     Person *p = [[Person alloc] init];
    3     Dog *d = [[Dog alloc] init];
    4    
    5     p.obj = d; // 警告,Dog没有遵守MyProtocol2协议
    6    
    7     return 0;
    8 }
     
    7.@protocol
    在.h可以先使用@protocol声明协议,在.m中再#import进来protocol的.h文件
    ==========Person.h=====================
     1 #import <Foundation/Foundation.h>
     2 
     3 @protocol MyProtocol;
     4 @protocol MyProtocol2;
     5 
     6 @interface Person : NSObject <MyProtocol]] >
     7 
     8 @property(nonatomic, strong) id<MyProtocol2> obj;
     9 
    10 @end
     
    ==========Person.m=====================
     1 #import "Person.h"
     2 #import "MyProtocol.h"
     3 #import "MyProtocol2.h"
     4 
     5 @implementation Person
     6 - (void)test1
     7 {
     8    
     9 }
    10 
    11 - (void)test3
    12 {
    13    
    14 }
    15 
    16 @end
     
     
  • 相关阅读:
    flex
    IOCP三:多个接收
    IOCP三:多个接收
    IOCP二:同时发送和接收
    IOCP二:同时发送和接收
    IOCP一:AcceptEx
    IOCP一:AcceptEx
    字符串和数字相互转换
    字符串和数字相互转换
    QThread应用详解
  • 原文地址:https://www.cnblogs.com/hellovoidworld/p/4119381.html
Copyright © 2020-2023  润新知