协议与代理

 

//协议

//协议双方相互遵守的约定

//劳务合同 文件 卖身协议 就业协议

//

//计算机

//http udp tcp mail ftp smb pop usb3.0 usb2.0 usb1.0 spi uart i2c

 

//OC中的协议

//是一组方法的列表

 

 

 

//1. 制定协议:

//所有的类直接或者间接继承NSObject类

//所有的协议直接或者间接的遵守NSObject协议

 

//制定协议的一方不负责实现协议

//@protocol Coder <NSObject>

//- (void)writeCode;

//- (void)debugCode;

//- (void)report;

//@end

 

//2.遵守协议

//遵守多个协议用逗号隔开

//@interface Coder : NSObject <NSCopying, NSCoding,Coder>

//

//@end

 

//3.实现协议

//需要包含协议头文件

//实现协议中的方法

 

//4.使用协议

//需要判断协议是否实现

 

 

#import <Foundation/Foundation.h>

#import "Coder.h"

//遵守协议

@interface Programmer : Coder <Coder>

 

@property (nonatomic, assign)float weight;

@property (nonatomic, copy)NSString *palce;

 

@end

 

 

 

#import "Programmer.h"

 

@implementation Programmer

- (void)writeCode

{

    NSLog(@"作为一个程序员, 我在努力写代码中...");

}

 

- (void)debugCode

{

    NSLog(@"这个程序有点问题,我还需要调试调试");

}

 

- (void)report

{

    NSLog(@"项目完成, app可以上架了");

}

 

@end

 

 

 

 

#import <Foundation/Foundation.h>

 

@interface Coder : NSObject

 

@property (nonatomic, assign) NSInteger age;

@property (nonatomic, copy)NSString *name;

 

- (void)print;

 

@end

 

//制定协议

@protocol Coder <NSObject>

 

- (void)writeCode;

- (void)debugCode;

- (void)report;

 

@end

 

 

 

 

#import "Coder.h"

 

@implementation Coder

 

- (void)print

{

    NSLog(@"name = %@ , age = %ld", self.name, self.age);

}

@end

 

 

 

#import "Programmer.h"

 

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

    @autoreleasepool {

        Programmer *xiaoHua = [[Programmer alloc] init];

        [xiaoHua writeCode];

        [xiaoHua debugCode];

        [xiaoHua report];

        

        xiaoHua.name= @"小华";

    }

    return 0;

}

 

 

---------------------------------------------------------------------------分割---------------------------------------------------------------------

 

#import <Foundation/Foundation.h>

 

@interface Dog : NSObject<NSCoding>

{

    NSString *_name;

    NSInteger _age;

}

@property (nonatomic, copy)NSString *name;

@property (nonatomic, assign)NSInteger age;

 

@end

 

 

#import "Dog.h"

 

@implementation Dog

 

@synthesize name = _name;

 

- (void)encodeWithCoder:(NSCoder *)coder

{//如果父类也遵守NSCoding协议 先用父类调用该方法

    //[super encodeWithCoder:coder];

    [coder encodeObject:self.name forKey:@"name"];

    [coder encodeInteger:self.age forKey:@"age"];

}

 

- (instancetype)initWithCoder:(NSCoder *)coder

{

    //self = [super initWithCoder:coder];

//    if (self) {

//        

//    }

    self.name = [coder decodeObjectForKey:@"name"];

    self.age = [coder decodeIntegerForKey:@"age"];

    return self;

}

@end

 

 

 

 

#import <Foundation/Foundation.h>

#import "Dog.h"

 

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

    @autoreleasepool {

        Dog *xiaoHei = [[Dog alloc] init];

        xiaoHei.name = @"小黑";

        xiaoHei.age = 12;

        

#if 0

      //归档

        //-[Dog encodeWithCoder:]

        BOOL ret = [NSKeyedArchiver archiveRootObject:xiaoHei toFile:@"/Users/zhangxueming/Desktop/test/dog.txt"];

        if (ret) {

            NSLog(@"归档成功");

        }

        else

        {

            NSLog(@"归档失败");

        }

        

#else

        Dog *dog = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/zhangxueming/Desktop/test/dog.txt"];

        

        NSLog(@"name = %@ age = %ld",dog.name, dog.age);

#endif

    }

    return 0;

}

 

 

---------------------------------------------------------------------------分割---------------------------------------------------------------------

 

#import <Foundation/Foundation.h>

#import "Teacher.h"

 

@interface QFTeacher : NSObject <Teacher>

 

@property (nonatomic, copy)NSString *name;

@property (nonatomic, assign)NSInteger age;

 

@end

 

 

#import "QFTeacher.h"

 

@implementation QFTeacher

 

- (void)teachCourse

{

    NSLog(@"老师正在教课");

}

 

- (void)writeCode

{

    NSLog(@"老师正在写代码");

}

 

//- (void)showCode

//{

//    NSLog(@"老师在演示代码");

//}

 

- (void)readyCourse

{

    NSLog(@"老师在备课");

}

 

- (NSString *)description

{

    return [NSString stringWithFormat:@"name = %@ age = %ld", self.name, self.age];

}

 

@end

 

 

 

 

#import <Foundation/Foundation.h>

 

@protocol Teacher <NSObject>

 

@required//(缺省)

//必须

- (void)teachCourse;

- (void)readyCourse;

@optional

//可选

- (void)writeCode;

- (void)showCode;

 

@end

 

 

 

#import <Foundation/Foundation.h>

#import "QFTeacher.h"

 

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

    @autoreleasepool {

        QFTeacher *xiaoWang = [[QFTeacher alloc] init];

        xiaoWang.name = @"小王";

        xiaoWang.age = 30;

        

        //使用协议中的方法

        if([xiaoWang respondsToSelector:@selector(writeCode)])

        {

            [xiaoWang performSelector:@selector(writeCode)];

        }

        if ([xiaoWang respondsToSelector:@selector(showCode)]) {

            [xiaoWang performSelector:@selector(showCode)];

        }

        //[xiaoWang showCode];

        [xiaoWang readyCourse];

        [xiaoWang teachCourse];

        

        NSLog(@"%@", xiaoWang);

        

    }

    return 0;

}

 

 

---------------------------------------------------------------------------分割---------------------------------------------------------------------

#import <Foundation/Foundation.h>

#import "Divver.h"

#import "Subber.h"

#import "Multer.h"

#import "Adder.h"

 

@interface Caluater : NSObject <Subber,Divver,Adder,Multer>

{

    NSInteger _num;

}

 

@property (nonatomic, assign)NSInteger num;

 

@end

 

 

#import "Caluater.h"

 

@implementation Caluater

 

- (NSInteger)adderAnum:(NSInteger)aNumber

{

    return self.num + aNumber;

}

- (NSInteger)subberAnum:(NSInteger)aNumber

{

    return self.num - aNumber;

}

- (NSInteger)multerAnum:(NSInteger)aNumber

{

    return self.num * aNumber;

}

- (NSInteger)diverAnum:(NSInteger)aNumber

{

    return self.num / aNumber;

}

 

@end

 

 

 

#import <Foundation/Foundation.h>

 

@protocol Divver <NSObject>

 

- (NSInteger)diverAnum:(NSInteger)aNumber;

@end

 

 

#import <Foundation/Foundation.h>

 

@protocol Subber <NSObject>

 

- (NSInteger)subberAnum:(NSInteger)aNumber;

 

@end

 

 

#import <Foundation/Foundation.h>

 

@protocol Multer <NSObject>

 

- (NSInteger)multerAnum:(NSInteger)aNumber;

 

@end

 

 

#import <Foundation/Foundation.h>

 

@protocol Adder <NSObject>

 

- (NSInteger)adderAnum:(NSInteger)aNumber;

 

@end

 

 

 

#import <Foundation/Foundation.h>

#import "Caluater.h"

 

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

    @autoreleasepool {

        Caluater *num = [[Caluater alloc] init];

        num.num = 10;

        NSLog(@"num = %ld", [num adderAnum:10]);

        NSLog(@"num = %ld", [num multerAnum:10]);

        NSLog(@"num = %ld", [num diverAnum:10]);

        NSLog(@"num = %ld", [num subberAnum:10]);

    }

    return 0;

}

 

 

 

 

---------------------------------------------------------------------------分割---------------------------------------------------------------------

#import <Foundation/Foundation.h>

 

#import "ClassA.h"

 

@interface ClassB : NSObject

{

    ClassA *_numA;

}

 

@property (nonatomic,retain)ClassA *numA;

 

@end

 

 

#import "ClassB.h"

 

@implementation ClassB

 

@end

 

 

 

#import <Foundation/Foundation.h>

//#import "ClassB.h"

@class ClassB;//打断头文件循环包含

 

@interface ClassA : NSObject

{

    ClassB *_numB;

}

 

@property (nonatomic,retain)ClassB *numB;

 

- (void)print;

 

 

@end

 

 

#import "ClassA.h"

//#import "ClassB.h"

@implementation ClassA

 

- (void)print

{

    NSLog(@"%@",self.numB);

}

@end

 

 

 

#import <Foundation/Foundation.h>

 

//@class

//只是引用一个类的类型

//如果在.m文件中引用头文件的方法那么还需要再用#import包含头文件

 

//#import

//实例变量及方法的声明引用类的类型

 

 

//A->B->C,F->D->E

 

//A.h->B.h

//B.h->A.h

//@class

 

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

    @autoreleasepool {

        

        

    }

    return 0;

}

 

 

 

 

 

---------------------------------------------------------------------------分割---------------------------------------------------------------------

#import <Foundation/Foundation.h>

#import "Dog.h"

 

@interface Person : NSObject

{

    Dog *_dog;

}

 

@property(nonatomic, retain)Dog *dog;

 

- (void)go;

 

@end

 

 

 

#import "Person.h"

 

@implementation Person

 

- (void)go

{

    [self.dog bark];

}

 

@end

 

 

 

#import <Foundation/Foundation.h>

 

@interface Dog : NSObject

 

- (void)bark;//恐吓

 

@end

 

 

 

#import "Dog.h"

 

@implementation Dog

 

- (void)bark

{

    NSLog(@"Wang wang wang ...");

}

 

@end

 

 

 

#import <Foundation/Foundation.h>

#import "Person.h"

#import "Dog.h"

 

//代理模式

//

//生活中的代理

//明星打官司

//明星<-->律师   以明星的角度来看, 律师是明星的代理(正向代理)

//   以律师的角度来看, 明星是律师的代理(反向代理)

 

 

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

    @autoreleasepool {

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

        Dog *xiaoBai = [[Dog alloc] init];

        xiaoXin.dog = xiaoBai;

        [xiaoXin go];

    }

    return 0;

}

 

 

 

---------------------------------------------------------------------------分割---------------------------------------------------------------------

#import <Foundation/Foundation.h>

 

 

@interface Person : NSObject

{

    id _delegate;

}

 

@property (nonatomic, retain) id delegate;

- (void)go;

 

@end

 

 

 

#import "Person.h"

#import "Dog.h"

#import "Cat.h"

 

@implementation Person

 

- (void)go

{

    [self.delegate bark];

}

 

 

@end

 

 

 

#import <Foundation/Foundation.h>

 

@interface Cat : NSObject

 

- (void)bark;

 

@end

 

 

 

#import "Cat.h"

 

@implementation Cat

 

- (void)bark

{

    NSLog(@"Miao miao miao ... ");

}

 

@end

 

 

#import <Foundation/Foundation.h>

 

@interface Dog : NSObject

 

- (void)bark;

 

@end

 

 

 

#import "Dog.h"

 

@implementation Dog

 

- (void)bark

{

 

    NSLog(@"Wang wang wang ...");

}

 

@end

 

 

 

#import <Foundation/Foundation.h>

#import "Person.h"

#import "Dog.h"

#import "Cat.h"

 

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

    @autoreleasepool {

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

        Dog *xiaoBai = [[Dog alloc] init];

        Cat *xiaoTom = [[Cat alloc] init];

        xiaoXin.delegate = xiaoBai;

        [xiaoXin go];

        xiaoXin.delegate = xiaoTom;

        [xiaoXin go];

        

    }

    return 0;

}

 

 

 

---------------------------------------------------------------------------分割---------------------------------------------------------------------

#import <Foundation/Foundation.h>

 

@protocol ProtectDelegate <NSObject>

 

- (void)bark;

 

@end

 

 

#import <Foundation/Foundation.h>

#import "ProtectDelegate.h"

 

@interface Person : NSObject

{

    id <ProtectDelegate>_delegate;//_delegate 指向的是遵守ProtectDelegate协议的对象

}

//setter getter方法展开也应该遵守ProtectDelegate协议

@property (nonatomic,retain) id <ProtectDelegate>delegate;

 

- (void)go;

 

@end

 

 

 

#import "Person.h"

 

@implementation Person

 

- (void)go

{

    [self.delegate bark];

}

@end

 

 

#import <Foundation/Foundation.h>

#import "ProtectDelegate.h"

 

@interface Cat : NSObject <ProtectDelegate>

 

@end

 

 

 

#import "Cat.h"

 

@implementation Cat

 

- (void)bark

{

    NSLog(@"Miao miao miao ...");

}

@end

 

 

#import <Foundation/Foundation.h>

#import "ProtectDelegate.h"

 

@interface Dog : NSObject<ProtectDelegate>

 

@end

 

 

 

#import "Dog.h"

 

@implementation Dog

 

- (void)bark

{

    NSLog(@"Wang wang wang ... ");

}

@end

 

 

 

#import <Foundation/Foundation.h>

#import "Dog.h"

#import "Cat.h"

#import "Person.h"

 

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

    @autoreleasepool {

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

        Cat *xiaoTom = [[Cat alloc] init];

        Dog *xiaoBai = [[Dog alloc] init];

        xiaoXin.delegate = xiaoBai;

        [xiaoXin go];

        xiaoXin.delegate = xiaoTom;

        [xiaoXin go];

    }

    return 0;

}

 

 

 

 

---------------------------------------------------------------------------分割---------------------------------------------------------------------

#import <Foundation/Foundation.h>

#import "ProtectDelegate.h"

 

@interface Person : NSObject

{

    id <ProtectDelegate>_delegate;

    NSInteger _count;

}

 

@property (nonatomic, retain) id <ProtectDelegate>delegate;

@property (nonatomic, assign) NSInteger count;

 

- (void)go;

 

 

@end

 

 

#import "Person.h"

 

@implementation Person

 

- (void)go

{

    [self.delegate bark:self.count];

}

 

 

@end

 

 

 

 

#import <Foundation/Foundation.h>

#import "ProtectDelegate.h"

 

@interface Cat : NSObject <ProtectDelegate>

 

@end

 

 

#import "Cat.h"

 

@implementation Cat

- (void)bark:(NSInteger)count

{

    for (NSInteger i=0; i<count; i++) {

        NSLog(@"Miao miao miao ...");

    }

}

@end

 

 

 

#import <Foundation/Foundation.h>

#import "ProtectDelegate.h"

@interface Dog : NSObject <ProtectDelegate>

 

@end

 

 

#import "Dog.h"

 

@implementation Dog

 

- (void)bark:(NSInteger)count

{

    for (NSInteger i=0; i<count; i++) {

        NSLog(@"Wang wang wang ...");

    }

}

 

@end

 

 

#import <Foundation/Foundation.h>

 

@protocol ProtectDelegate <NSObject>

 

- (void)bark:(NSInteger)count;

 

@end

 

 

 

 

#import <Foundation/Foundation.h>

#import "Person.h"

#import "Dog.h"

#import "Cat.h"

 

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

    @autoreleasepool {

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

        xiaoXin.count = 10;

        Cat *xiaoTom = [[Cat alloc] init];

        Dog *xiaoBai = [[Dog alloc] init];

        xiaoXin.delegate = xiaoBai;

        [xiaoXin go];

        xiaoXin.delegate = xiaoTom;

        [xiaoXin go];

        

    }

    return 0;

}

 

---------------------------------------------------------------------------分割---------------------------------------------------------------------

 

#import <Foundation/Foundation.h>

#import "ProtectDelegate.h"

 

@interface Person : NSObject

{

    id <ProtectDelegate>_delegate;

}

 

@property (nonatomic, retain)id <ProtectDelegate>delegate;

 

- (void)go;

 

- (void)report:(NSInteger)count;

 

@end

 

 

#import "Person.h"

 

@implementation Person

 

- (void)go

{

    [self.delegate bark];

}

 

- (void)report:(NSInteger)count

{

    NSLog(@"killed %ld devils", count);

}

 

@end

 

 

#import <Foundation/Foundation.h>

 

@protocol ProtectDelegate <NSObject>

 

- (void)bark;

 

@end

 

 

#import <Foundation/Foundation.h>

#import "ProtectDelegate.h"

@class Person;

 

@interface Dog : NSObject <ProtectDelegate>

{

    Person *_master;

}

 

@property (nonatomic, retain)Person *master;

 

@end

 

 

 

 

#import "Dog.h"

#import "Person.h"

 

@implementation Dog

 

 

- (void)bark

{

    NSLog(@"Wang wang wang ...");

    [self.master report:arc4random()%100];

}

 

@end

 

 

 

 

#import <Foundation/Foundation.h>

#import "Dog.h"

#import "Person.h"

 

//正向传值: 是因为person 类中 有代理对象指针

//反向传值需要在Dog类中有Person类对象指针

 

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

    @autoreleasepool {

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

        Dog *xiaoBai = [[Dog alloc] init];

        xiaoXin.delegate = xiaoBai;

        xiaoBai.master = xiaoXin;

        [xiaoXin go];

    }

    return 0;

}

 

 

 

---------------------------------------------------------------------------分割---------------------------------------------------------------------

#import <Foundation/Foundation.h>

#import "ReceiveDelegateReport.h"

 

@interface God : NSObject <ReceiveDelegateReport>

 

 

@end

 

 

 

 

#import <Foundation/Foundation.h>

#import "ReceiveDelegateReport.h"

 

@interface Person : NSObject <ReceiveDelegateReport>

 

 

@end

 

 

 

#import "Person.h"

 

@implementation Person

 

- (void)killed:(NSInteger)count

{

    NSLog(@"killed %ld devils", count);

}

 

@end

 

 

 

#import "God.h"

 

@implementation God

 

- (void)killed:(NSInteger)count

{

    NSLog(@"killed %ld devils", count);

}

 

@end

 

 

 

 

#import <Foundation/Foundation.h>

#import "ReceiveDelegateReport.h"

 

@interface Dog : NSObject

{

    id <ReceiveDelegateReport>_master;

}

 

@property (nonatomic, retain)id <ReceiveDelegateReport>master;

 

- (void)report;

 

@end

 

 

 

 

#import "Dog.h"

 

@implementation Dog

 

- (void)report

{

    NSLog(@"Wang wang wang ...");

    [self.master killed:arc4random()%100];

}

 

@end

 

 

 

 

#import <Foundation/Foundation.h>

 

@protocol ReceiveDelegateReport <NSObject>

 

- (void)killed:(NSInteger)count;

 

@end

 

 

 

 

#import <Foundation/Foundation.h>

#import "Dog.h" 

#import "God.h"

#import "Person.h"

 

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

    @autoreleasepool {

        Dog *xiaoBai = [[Dog alloc] init];

        God *littleGod = [[God alloc] init];

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

        xiaoBai.master = littleGod;

        [xiaoBai report];

        xiaoBai.master = xiaoXin;

        [xiaoBai report];

    }

    return 0;

}

 

让明天,不后悔今天的所作所为
原文地址:https://www.cnblogs.com/-yun/p/4335920.html