16-Protocol的定义与使用及block

协议单独写在一个文件中


//

//  MyProtocal.h

//  Block和协议

//

//  Created by amesty on 15-1-9.

//  Copyright (c) 2015 itcast. All rights reserved.

//


#import <Foundation/Foundation.h>


//定义了一个名叫protocal 的协议

//协议方法声明中的关键字

//

//1required (默认)要求实现,若没有实现则警告但不报错

//

//2Optional 不要求实现

@protocol MyProtocal <NSObject>

@required   //  要求实现,若没有实现则警告但不报错

-(void) test;

@optional   //  不要求实现

-(void) test2;

@end

/*

 

 1.协议的定义

 

 @protocol 协议名称 <NSObject>

 

 //方法声明列表

 

 @end;

 

 2.如何遵守协议

 

 1)类遵守协议

 

 @protocol 类名:父类名 <协议名称1,协议名称2>

 

 @end

 

 2)协议遵守协议

 

 @protocol 协议名称 <其他协议名称>

 

 @end;

 

 3.协议方法声明中的关键字

 

 1required (默认)要求实现,若没有实现则警告但不报错

 

 2Optional 不要求实现

 

 4.定义变量时遵守协议的限制

 

 类名<协议名称> *变量名    NSObject<.Myprotocol> *obj;

 

 Id  <协议名称>  变量名   id  <.Myprotocol> obj1;

 

     NSObject<MyProtocal3> * obj3=[[Person alloc]init];

    id <MyProtocal3> obj4=[[Person alloc]init];

 

 

 5.Property中声明的属性也可以做遵守协议的限制

 

 @property (nonatomic ,strong ) 类名<协议名称> *属性名;

 

 @property (nonatomic ,strong ) id<协议名称>  属性名;

 

 @property (nonatomic,strong)id<MyProtocal2> obj1; //id代表任何类型的对象 obj1对象名称

 @property(nonatomic,strong)NiceDog * littledog;

 

 

 6.补充知识:(如果协议需要遵守的只有一个类就写在这个类中,需要多个类实现,就需要写在单独文件中)

 协议本身写在.h头文件中,但也可以定义在任何地方。

 1>:当这个协议只有这个类使用遵守时,一般把协议写在这个类里边.

 2>:当这个协议需要多个类去实现时,就写在外边单独的文件中。

 */


============



//  Person.h

//  Block和协议

//

//  Created by amesty on 15-1-9.

//  Copyright (c) 2015 itcast. All rights reserved.

//


#import <Cocoa/Cocoa.h>

#import "MyProtocal2.h"

#import "MyProtocal3.h"

@class NiceDog;

@protocol MyProtocal2,MyProtocal3;

//@protocol MyProtocal3;

//只要某个类遵守了这个协议,就拥有了这个协议中的所有方法声明。

// 继承父类

// <>遵守协议


@interface Person : NSObject // <MyProtocal2,MyProtocal3>

@property (nonatomic,strong)id<MyProtocal2> obj1; //id代表任何类型的对象 obj1对象名称

@property(nonatomic,strong)NiceDog * littledog;

@end


===========

//  Person.m

//  Block和协议

//

//  Created by amesty on 15-1-9.

//  Copyright (c) 2015 itcast. All rights reserved.

//


#import "Person.h"

#import"MyProtocal2.h"

#import"MyProtocal3.h"

@implementation Person

-(void) test

{}

-(void)haha2

{}

-(void)here

{}

@end

=============

//  MyProtocal2.h

//  Block和协议

//

//  Created by amesty on 15-1-9.

//  Copyright (c) 2015 itcast. All rights reserved.

//


#import <Foundation/Foundation.h>


@protocol MyProtocal2 <NSObject>

@optional

-(void)haha;

@required

-(void)haha2;

@end


=========

//  MyProtocal3.h

//  Block和协议

//

//  Created by amesty on 15-1-9.

//  Copyright (c) 2015 itcast. All rights reserved.

//


#import <Foundation/Foundation.h>

#import "MyProtocal.h"

@protocol MyProtocal3 <MyProtocal>


@required

-(void)here;

@end


===============


//  Dog.h

//  Block和协议

//

//  Created by amesty on 15-1-9.

//  Copyright (c) 2015 itcast. All rights reserved.

//


#import <Cocoa/Cocoa.h>

//#import"MyProtocal2.h"

@protocol MyProtocal2;

@interface Dog : NSObject //<MyProtocal2>

@end


=======

//  Dog.m

//  Block和协议

//

//  Created by amesty on 15-1-9.

//  Copyright (c) 2015 itcast. All rights reserved.

//


#import "Dog.h"

#import "MyProtocal2.h"

@implementation Dog

-(void)haha2

{}

@end


==============


//  NiceDog.h

//  Block和协议

//

//  Created by amesty on 15-1-9.

//  Copyright (c) 2015 itcast. All rights reserved.

//


#import "Dog.h"


@interface NiceDog : Dog


@end


@interface NiceDog (Add) // 如果分类的内容比较少时,可以将分类放在类中

-(void)addTest;

@end


=========

//  NiceDog.m

//  Block和协议

//

//  Created by amesty on 15-1-9.

//  Copyright (c) 2015 itcast. All rights reserved.

//


#import "NiceDog.h"


@implementation NiceDog


@end


@implementation NiceDog (Add) // 如果分类的内容比较少时,可以将分类放在类中

-(void)addTest

{}

@end


============

//  NiceDog+CF.h

//  Block和协议

//

//  Created by amesty on 15-1-9.

//  Copyright (c) 2015 itcast. All rights reserved.

//


#import "NiceDog.h"


@interface NiceDog (CF)


@end

=======

//  NiceDog+CF.m

//  Block和协议

//

//  Created by amesty on 15-1-9.

//  Copyright (c) 2015 itcast. All rights reserved.

//


#import "NiceDog+CF.h"


@implementation NiceDog (CF)


@end

==============

//

//  main.m

//  Block和协议

//

//  Created by amesty on 15-1-9.

//  Copyright (c) 2015 itcast. All rights reserved.

//

/*

  block 用来保存一段代码块

  block的标志是 ^

  BOLCK和函数的相似性:

 1)可以保存代码

 2)有返回值

 3)有形参

 4)调用方式一样。

  

 

 利用typedef定义block类型(和指向函数的指针很像)

 

 Typedef int(^MyBlock)(int ,int);

 

 以后就可以利用这种类型来定义block变量了。

 

 

 Block访问外部变量

 

 1Block内部可以访问外部变量;

 

 2)默认情况下,Block内部不能修改外部的局部变量

 

 3)给局部变量加上__block关键字,则这个局部变量可以在block内部进行修改。


 */


#import <Foundation/Foundation.h>

#import "Person.h"

#import "Dog.h"

#import"NiceDog.h"

typedef int(^Mybloc)(int,int);

// 利用typedef定义block类型(和指向函数的指针很像)

int main(int argc, const char * argv[])

{

//    NSObject *obj=[[NSObject alloc]init];

//    NSObject  *obj2=@"123123";

//    要求object3object4要遵守MyProtocol这个协议

//    NSObject<MyProtocal3> * obj3=[[Person alloc]init];

//    id <MyProtocal3> obj4=[[Person alloc]init];

    

    Person *p=[[Person alloc]init];

    p.littledog=[[NiceDog alloc]init];

    

    return 0;

}

// 利用typedef定义block类型(和指向函数的指针很像)

void test2()

{

    Mybloc multiply=^(int a,int b)

    {

        return a*b;

    };

    int result=multiply(4,5);

    NSLog(@"%d",result);

    

    Mybloc divbloc=^(int a,int b)

    {

        return a/b;

    };

    int result1=(int)divbloc(4,5);

    NSLog(@"%d",result1);



}


void tes()

{


    @autoreleasepool {

        //定义block  myblockblock变量名称

        //没有形参,没有返回值的block

        void (^myblock)()=^(){

            NSLog(@"-----------------");

            

            NSLog(@"-----------------");

        };

        //利用block变量名myblock调用block的内部代码

        myblock();

        // insert code here...

        NSLog(@"Hello, World!");

        

    }

   

}


// block块名称和指针函数类似

/*

 int sum(int a,int b)

 {

 return a+b;

 }

 

 int (*p)(int ,int )=sum;

 int result=p(10,19);

 NSLog(@"%d",result);

 

 */



//指定参数及返回值类型的block

/*

 int(^sumbloc)(int ,int)=^(int a,int b)

 {

 return a+b;

 

 };

 int (^minusbloc)(int,int)= ^(int a,int b)

 {  return a>b?a-b:b-a;

 };

 

 

 int result=sumbloc(10,19);

 NSLog(@"%d",result);

 NSLog(@"%d",minusbloc(10,19));

 

 void (^forbloc)(int) = ^(int n){

 

 for(int i=0;i<n;i++)

 

 NSLog(@"-----------------");

 

 };

 

 // forbloc(5);

*/



/*

 利用typedef定义block类型(和指向函数的指针很像)

 

 Mybloc multiply=^(int a,int b)

 {

 return a*b;

 };

 int result=multiply(4,5);

 NSLog(@"%d",result);

 

 Mybloc divbloc=^(int a,int b)

 {

 return a/b;

 };

 int result1=(int)divbloc(4,5);

 NSLog(@"%d",result1);


 */

===============================

protocol协议申明和实现和类在一起

//  main.m

//  Protocol

//

//  Created by amesty on 15-1-9.

//  Copyright (c) 2015 itcast. All rights reserved.


#import <Foundation/Foundation.h>

#import "Button.h"

#import "ButtonListener.h"

#import "MyListener.h"


int main(int argc, const char * argv[])

{


    @autoreleasepool {

        // 初始化一个按钮

        Button *button = [[[Button allocinitautorelease];

        

        // 初始化一个按钮的监听器

//        ButtonListener *listener = [[[ButtonListener alloc] init] autorelease];

        MyListener *listener = [[[MyListener allocinitautorelease];

        

        // 设置按钮的监听器

        button.delegate = listener;

        NSLog(@"button:%@", button);

        // 点击按钮

        [button click];

        

        Button *button2 = [[[Button allocinitautorelease];

        button2.delegate = listener;

        NSLog(@"button2:%@", button2);

        // 点击button2

        [button2 click];

    }

    return 0;

}

========

//  Button.h

//  Protocol

//

//  Created by amesty on 15-1-9.

//  Copyright (c) 2015 itcast. All rights reserved.


#import <Foundation/Foundation.h>

@class Button;


// <>代表实现某个协议

@protocol ButtonDelegate <NSObject>

- (void)onClick:(Button *)btn;

@end



@interface Button : NSObject


// delegate就是按钮的监听器

@property (nonatomicretainid<ButtonDelegate> delegate;

//Property中声明的属性也可以做遵守协议的限制


//@property (nonatomic ,strong ) 类名<协议名称> *属性名;


//@property (nonatomic ,strong ) id<协议名称>  属性名;

// 点击按钮

- (void)click;

@end


=======

//  Button.m

//  Protocol

//

//  Created by amesty on 15-1-9.

//  Copyright (c) 2015 itcast. All rights reserved.

//


#import "Button.h"


@implementation Button



- (void)dealloc {

    [_delegate release];

    

    [super dealloc];

}


- (void)click {

    // 如果_delegate实现了onClick:这个方法

    if ( [_delegate respondsToSelector:@selector(onClick:)] ) {

        // 按钮被点击了,就应该通知监听器.并且告诉监听器哪个按钮被点击了

        [_delegate onClick:self];

    } else {

        NSLog(@"监听器并没有实现onClick:方法");

    }

}

@end

==========


//

//  ButtonListener.h

//  Protocol

//

//  Created by amesty on 15-1-9.

//  Copyright (c) 2015 itcast. All rights reserved.

//


#import <Foundation/Foundation.h>


// 对协议进行提前声明,跟@class的用途是一致的

@protocol ButtonDelegate;


@interface ButtonListener : NSObject <ButtonDelegate>


@end

========

//  ButtonListener.m

//  Protocol

//

//  Created by amesty on 15-1-9.

//  Copyright (c) 2015 itcast. All rights reserved.

//


#import "ButtonListener.h"

#import "Button.h"


@implementation ButtonListener

- (void)onClick {

    NSLog(@"ButtonListener已经监听到按钮被点击了");

}

@end

============

//  MyListener.h

//  Protocol

//

//  Created by amesty on 15-1-9.

//  Copyright (c) 2015 itcast. All rights reserved.

//


#import <Foundation/Foundation.h>


@protocol ButtonDelegate;


@interface MyListener : NSObject <ButtonDelegate>


@end

===========

//  MyListener.m

//  Protocol

//

//  Created by amesty on 15-1-9.

//  Copyright (c) 2015 itcast. All rights reserved.

//


#import "MyListener.h"

#import "Button.h"


@implementation MyListener

- (void)onClick {

    NSLog(@"MyListener已经监听到按钮被点击了");

}


- (void)onClick:(Button *)btn {

    NSLog(@"MyListener已经监听到按钮-%@被点击了", btn);

}

@end


====================================








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值