年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2053|回复: 0

第二十一讲:Objective-C基本数据结构之协议

[复制链接]
  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

    发表于 2014-1-20 21:40:40 | 显示全部楼层 |阅读模式
    本帖最后由 Sian 于 2014-1-20 22:11 编辑

    1、什么是协议
    2、如何定义协议
    3、如何实现协议
    MyProtocol.h
    1. //
    2. //  MyProtocol.h
    3. //  Protocol
    4. //
    5. //  Created by yusian on 14-1-20.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import <Foundation/Foundation.h>
    9. @protocol MyProtocol <NSObject>
    10. @required
    11. - (int)printValue:(int)value1 andValue:(int)value2;
    12. @optional
    13. - (void)print:(int)value;
    14. @end
    复制代码

    MyTest.h
    1. //
    2. //  MyTest.h
    3. //  Protocol
    4. //
    5. //  Created by yusian on 14-1-20.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import <Foundation/Foundation.h>
    9. #import "MyProtocol.h"
    10. @interface MyTest : NSObject <MyProtocol>
    11. - (void)showInfo;
    12. @end
    复制代码
    MyTest.m
    1. //
    2. //  MyTest.m
    3. //  Protocol
    4. //
    5. //  Created by yusian on 14-1-20.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import "MyTest.h"
    9. @implementation MyTest
    10. - (void)showInfo
    11. {
    12.     NSLog(@"showInfo is calling");
    13. }
    14. - (int)printValue:(int)value1 andValue:(int)value2
    15. {
    16.     NSLog(@"printValue is %d, printValue is %d",value1,value2);
    17.     return 0;
    18. }
    19. - (void)print:(int)value
    20. {
    21.     NSLog(@"print is %d",value);
    22. }
    23. @end
    复制代码
    main.m
    1. //
    2. //  main.m
    3. //  Protocol
    4. //
    5. //  Created by yusian on 14-1-20.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import <Foundation/Foundation.h>
    9. #import "MyTest.h"
    10. #import "MyProtocol.h"
    11. int main(int argc, const char * argv[])
    12. {
    13.     @autoreleasepool {
    14.         
    15.         MyTest * myTest = [[MyTest alloc] init];
    16.         
    17.         [myTest showInfo];
    18.         
    19. //**************************************************
    20. //实现方法一:
    21.         NSLog(@"实现方法一:");
    22.         [myTest printValue:10 andValue:20];
    23.         
    24.         SEL sel = @selector(print:);
    25.         if ([myTest respondsToSelector:sel]) {
    26.             [myTest print:100];
    27.         }
    28.         
    29.         [myTest release];
    30. //**************************************************
    31. //实现方法二:
    32.         NSLog(@"实现方法二:");
    33.         id <MyProtocol> myTest_Protocol = [[MyTest alloc] init];
    34.         
    35.         [myTest_Protocol printValue:100 andValue:200];
    36.         
    37.         if ([myTest_Protocol respondsToSelector:@selector(print:)]) {
    38.             [myTest_Protocol print:1000];
    39.         }
    40.         [myTest_Protocol release];
    41. //**************************************************
    42.         
    43.     }
    44.     return 0;
    45. }
    复制代码
    输出结果:
    1. 2014-01-20 21:38:06.164 Protocol[9350:303] showInfo is calling
    2. 2014-01-20 21:38:06.165 Protocol[9350:303] 实现方法一:
    3. 2014-01-20 21:38:06.166 Protocol[9350:303] printValue is 10, printValue is 20
    4. 2014-01-20 21:38:06.166 Protocol[9350:303] print is 100
    5. 2014-01-20 21:38:06.166 Protocol[9350:303] 实现方法二:
    6. 2014-01-20 21:38:06.167 Protocol[9350:303] printValue is 100, printValue is 200
    7. 2014-01-20 21:38:06.167 Protocol[9350:303] print is 1000
    8. Program ended with exit code: 0
    复制代码

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-5-5 19:50 , Processed in 0.043621 second(s), 18 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表