zoukankan      html  css  js  c++  java
  • IOS开发之----协议与委托(Protocol and Delegate) 实例解析

    1 协议:

    协议,类似于Java或C#语言中的接口,它限制了实现类必须拥有哪些方法。
    它是对对象行为的定义,也是对功能的规范。

    在写示例之前我给大家说下@required和@optional这两个关键字

    他们两个是在声明协议的时候用到,@required是必须实现的方法,要不会报黄色警告.@optional是可选实现!实现还是不实现都不会报警告!

    示例:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    // GoodChild.h

    #import

    @protocol GoodChild

    -(void)filialPiety;

    @end

     

    1
    2
    3
    4
    5
    6
    7
    8
    // Student.h

    #import
    #import "GoodChild.h"
    //注意实现协议的语法。
    @interface Student : NSObject

    @end

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    // Student.m
    // protocol
    //
    // Created by sxt on 11-10-23.
    // Copyright 2011年 __MyCompanyName__. All rights reserved.
    //

    #import "Student.h"

    @implementation Student

    (id)init
    {
    self [super init];
    if (self{
    // Initialization code here.
    }

    return self;
    }
    -(void)filialPiety{
    NSLog(@"孝敬父母。。");
    }
    @end

    此例中定义了一个协议GoodChild,类Student实现了此协议,所以必须有filialPiety方法。
    每个类虽只有一个父类,但可以实现多个协议,以逗号隔开便可。语法如下:

    1
    2
    3
    @interface Student : NSObject<协议1,协议2>

    @end

    2 委托:

    委托是objC中使用非常频繁的一种设计模式,它的实现与协议的使用是分不开的,让我们看一个综合示例:
    小公司老板日常的工作是管理公司、教导新员工、发工资与接电话。
    其中管理公司、教导新员工是老板要亲为的。
    而发工资与接电话老板希望招聘一个秘书来帮忙,于是对秘书的要求就是要略懂出纳发工资,要能帮助领导接电话。 而这两项要求便是协议,对类功能的限定。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    // SecProtocol.h

    #import

    @protocol SecProtocol
    //发工资
    -(void)payoff;
    //接电话
    -(void)tel;

    @end

    然后定义一个秘书类

    1
    2
    3
    4
    5
    6
    7
    // Sec.h

    #import
    #import "SecProtocol.h"
    @interface Sec : NSObject

    @end

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    // Sec.m

    #import "Sec.h"

    @implementation Sec

    (id)init
    {
    self [super init];
    if (self{
    // Initialization code here.
    }

    return self;
    }

    -(void)payoff{
    NSLog(@"sec payoff");
    }

    -(void)tel{
    NSLog(@"sec tel");
    }

    @end

    紧接着是老板类:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    // Boss.h

    #import
    #import "SecProtocol.h"
    @interface Boss NSObject

    //此属性用于指定秘书对象,此对象必须实现SecProtocol协议。
    @property(nonatomic,retain) id detegate;
    //管理
    -(void)manage;
    //教导新员工
    -(void)teach;

    @end

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    // Boss.m
    #import "Boss.h"

    @implementation Boss

    @synthesize detegate=_detegate;

    (id)init
    {
    self [super init];
    if (self{
    // Initialization code here.
    }

    return self;
    }

    -(void)manage{
    NSLog(@"boss manage");
    }

    -(void)teach{
    NSLog(@"boss teach");
    }

    -(void)payoff{

    NSAutoreleasePool *p=[[NSAutoreleasePool alloc] init];
    [_detegate payoff];
    [p release];

    }

    -(void)tel{

    NSAutoreleasePool *p=[[NSAutoreleasePool alloc] init];
    [_detegate tel];
    [p release];

    }
    @end

    那么老板就具有这4个方法,当调用前2个时是自己完成功能,而调用后2个时则转为调用秘书的方法。
    此时我们跟秘书对象就叫做代理对象,代理模式的名字由来于此。
    最后调用测试下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    // main.m
    // delegate
    //
    // Created by sxt on 11-10-23.
    // Copyright 2011年 Jinlong Wei. All rights reserved.
    //

    #import
    #import "Boss.h"
    #import "Sec.h"
    int main (int argc, const char * argv[])
    {

    NSAutoreleasePool * pool [[NSAutoreleasePool alloc] init];

    //实例化老板对象
    Boss *boss=[[[Boss alloc] init] autorelease];
    //实例化秘书对象
    Sec *sec=[[[Sec alloc] init] autorelease];

    //设置老板的代理对象为秘书
    boss.detegate=sec;

    //调用4个方法。
    [boss payoff];
    [boss tel];
    [boss manage];
    [boss teach];
    [pool drain];
    return 0;
    }
  • 相关阅读:
    解决子元素浮动,父元素高度变小的问题
    解决子元素设置margintop,效果到父元素上的问题
    应用部署时自动推送企业微信提醒
    自定义Gradle Task实现一键批量部署Springboot应用
    springboot中@PostConstruct、CommandLineRunner、BeanPostProcessor执行顺序
    win7 关闭防火墙
    Android 4.1模拟器访问服务器接口
    Android开发教程 葵花宝典第一层 初识Android
    Android开发教程 葵花宝典第四层 控件之 Toast Spinner RadioButton
    Android开发教程 葵花宝典第三层 控件之 Button CheckBox EditText TextView
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/4973220.html
Copyright © 2011-2022 走看看