20150624_OC之代理及反向代理的简单例子

来源:互联网 发布:淘宝靠谱的宠物店 编辑:程序博客网 时间:2024/06/04 20:33

以下例子从例1到例6由浅到深说明代理:

例子1:

////  Dog.h//  IOS150624_ObjectiveC_代理5////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import <Foundation/Foundation.h>@interface Dog : NSObject- (void)bark;@end

////  Dog.m//  IOS150624_ObjectiveC_代理5////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import "Dog.h"@implementation Dog- (void)bark{    NSLog(@"汪汪汪汪");}@end

////  Person.h//  IOS150624_ObjectiveC_代理5////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import <Foundation/Foundation.h>#import "Dog.h"@interface Person : NSObject{    Dog *_delegate;}@property (nonatomic)Dog *delegate;- (void)go;@end

////  Person.m//  IOS150624_ObjectiveC_代理5////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import "Person.h"@implementation Person- (void)go{    [_delegate bark];}@end

////  main.m//  IOS150624_ObjectiveC_代理5////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import <Foundation/Foundation.h>#import "Person.h"//代理://买火车票  本人-->黄牛//小张找律师帮她打官司//以小张的角度:律师是小张的代理(正向代理)//一律师的角度:小张是律师的代理(反向代理)int main(int argc, const char * argv[]) {    @autoreleasepool {        //狗市xiaozhang的代理        Person *xiaoZhang = [[Person alloc] init];        Dog *dog = [[Dog alloc] init];        xiaoZhang.delegate = dog;        [xiaoZhang go];            }    return 0;}

例子2:

////  Cat.h//  IOS150624_ObjectiveC_代理6////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import <Foundation/Foundation.h>@interface Cat : NSObject- (void)bark;@end

////  Cat.m//  IOS150624_ObjectiveC_代理6////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import "Cat.h"@implementation Cat- (void)bark{    NSLog(@"喵喵叫");}@end

////  Dog.h//  IOS150624_ObjectiveC_代理6////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import <Foundation/Foundation.h>@interface Dog : NSObject- (void)bark;@end

////  Dog.m//  IOS150624_ObjectiveC_代理6////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import "Dog.h"@implementation Dog- (void)bark{    NSLog(@"汪汪叫");}@end

////  Person.h//  IOS150624_ObjectiveC_代理6////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import <Foundation/Foundation.h>@interface Person : NSObject{    id _delegate;}@property (retain,nonatomic)id delegate;- (void)go;@end

////  Person.m//  IOS150624_ObjectiveC_代理6////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import "Person.h"#import "Dog.h"#import "Cat.h"@implementation Person- (void)go{    [_delegate bark];//调用代理对象中的方法}@end

//  main.m//  IOS150624_ObjectiveC_代理6////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import <Foundation/Foundation.h>#import "Person.h"#import "Dog.h"#import "Cat.h"//这里的代理还不是真正意义上的代理int main(int argc, const char * argv[]) {    @autoreleasepool {        Person *xiaoZhang = [[Person alloc] init];        Dog *dog = [[Dog alloc] init];        Cat *cat = [[Cat alloc] init];        xiaoZhang.delegate = dog;        [xiaoZhang go];        xiaoZhang.delegate = cat;        [xiaoZhang go];    }    return 0;}

例子3:////  ProtectedDelegate.h//  IOS150624_ObjectiveC_代理7////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import <Foundation/Foundation.h>@protocol ProtectedDelegate <NSObject>- (void)bark;@end//———————————////  Dog.h//  IOS150624_ObjectiveC_代理7////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import <Foundation/Foundation.h>#import "ProtectedDelegate.h"@interface Dog : NSObject <ProtectedDelegate>@end//—————————————////  Dog.m//  IOS150624_ObjectiveC_代理7////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import "Dog.h"@implementation Dog- (void)bark{    NSLog(@"汪汪汪叫");}@end//—————————————////  Cat.h//  IOS150624_ObjectiveC_代理7////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import <Foundation/Foundation.h>#import "ProtectedDelegate.h"@interface Cat : NSObject <ProtectedDelegate>@end//———————————————////  Cat.m//  IOS150624_ObjectiveC_代理7////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import "Cat.h"@implementation Cat- (void)bark{    NSLog(@"喵喵喵叫");}@end//—————————————————////  Person.h//  IOS150624_ObjectiveC_代理7////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import <Foundation/Foundation.h>#import "ProtectedDelegate.h"@interface Person : NSObject{    id <ProtectedDelegate>_delegate;   //对象指针也遵守协议}@property (retain,nonatomic)id <ProtectedDelegate>delegate;- (void)go;@end//————————————————////  Person.m//  IOS150624_ObjectiveC_代理7////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import "Person.h"@implementation Person- (void)go{    [_delegate bark];}@end//————————————————////  main.m//  IOS150624_ObjectiveC_代理7////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import <Foundation/Foundation.h>#import "Person.h"#import "Dog.h"#import "Cat.h"//封装协议的代理int main(int argc, const char * argv[]) {    @autoreleasepool {        Person *xiaoZhang = [[Person alloc] init];        Dog *dog = [[Dog alloc] init];        Cat *cat = [[Cat alloc] init];        xiaoZhang.delegate = dog;        [xiaoZhang go];        xiaoZhang.delegate = cat;        [xiaoZhang go];    }    return 0;}例子4:代理正向传值:人向狗传递交的次数////  ProtectedDelegate.h//  IOS150624_ObjectiveC_代理正向传值8////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import <Foundation/Foundation.h>@protocol ProtectedDelegate <NSObject>- (void)bark:(NSInteger)count;@end//——————————————////  Dog.h//  IOS150624_ObjectiveC_代理正向传值8////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import <Foundation/Foundation.h>#import "ProtectedDelegate.h"@interface Dog : NSObject <ProtectedDelegate>@end//——————————————////  Dog.m//  IOS150624_ObjectiveC_代理正向传值8////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import "Dog.h"@implementation Dog- (void)bark:(NSInteger)count{    while (count) {        NSLog(@"汪");        count--;    }}@end//———————————————////  Person.h//  IOS150624_ObjectiveC_代理正向传值8////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import <Foundation/Foundation.h>#import "ProtectedDelegate.h"@interface Person : NSObject{    id <ProtectedDelegate>_delegate;}@property (retain,nonatomic)id <ProtectedDelegate>delegate;@property (assign,nonatomic)NSInteger count;- (void)go;@end//———————————————————////  Person.m//  IOS150624_ObjectiveC_代理正向传值8////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import "Person.h"@implementation Person- (void)go{    [_delegate bark:[self count]];}@end//———————————————————////  main.m//  IOS150624_ObjectiveC_代理正向传值8////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import <Foundation/Foundation.h>#import "Person.h"#import "Dog.h"int main(int argc, const char * argv[]) {    @autoreleasepool {        Person *xiaoZhang = [[Person alloc] init];        Dog *dog = [[Dog alloc] init];        xiaoZhang.count = 5;        xiaoZhang.delegate = dog;        [xiaoZhang go];    }    return 0;}例子5:反向传值:狗返回要的人数////  ProtectedDelegate.h//  IOS150624_ObjectiveC_代理反向传值9////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import <Foundation/Foundation.h>@protocol ProtectedDelegate <NSObject>- (void)bark;@end//———————————////  Dog.h//  IOS150624_ObjectiveC_代理反向传值9////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import <Foundation/Foundation.h>#import "ProtectedDelegate.h"#import "Person.h"@interface Dog : NSObject <ProtectedDelegate>@property (nonatomic)Person *master;    //指向主人的对象指针@end//————————————////  Dog.m//  IOS150624_ObjectiveC_代理反向传值9////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import "Dog.h"@implementation Dog- (void)bark{    NSLog(@"要人啦");    [self.master report:arc4random()%10+1];    //实现反向传值,将值反传给Person;对象指针指向谁就向谁汇报}@end//———————————————////  Person.h//  IOS150624_ObjectiveC_代理反向传值9////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import <Foundation/Foundation.h>#import "ProtectedDelegate.h"@interface Person : NSObject@property (assign,nonatomic)id <ProtectedDelegate>delegate;- (void)go;- (void)report:(NSInteger)count;@end//———————————————////  Person.m//  IOS150624_ObjectiveC_代理反向传值9////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import "Person.h"@implementation Person- (void)go{    [self.delegate bark];}- (void)report:(NSInteger)count{    NSLog(@"Kill %ld people",count);}@end//———————————————////  main.m//  IOS150624_ObjectiveC_代理反向传值9////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import <Foundation/Foundation.h>#import "Person.h"#import "Dog.h"//将狗咬的人数汇报给它的主人int main(int argc, const char * argv[]) {    @autoreleasepool {        Person *xiaoZhang = [[Person alloc] init];        Dog *dog = [[Dog alloc] init];        xiaoZhang.delegate = dog;   //狗成了人的咬人的代理        dog.master = xiaoZhang;     //人是狗的主人,对象指针指向其主人        [xiaoZhang go];    }    return 0;}例子6:真正的代理////  ReceiveReportDelegate.h//  IOS150624_ObjectiveC_真正的代理10////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import <Foundation/Foundation.h>@protocol ReceiveReportDelegate <NSObject>- (void)report:(NSInteger)count;@end//———————————////  Person.h//  IOS150624_ObjectiveC_真正的代理10////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import <Foundation/Foundation.h>#import "ReceiveReportDelegate.h"@interface Person : NSObject <ReceiveReportDelegate>@end//———————————////  Person.m//  IOS150624_ObjectiveC_真正的代理10////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import "Person.h"@implementation Person- (void)report:(NSInteger)count{    NSLog(@"Hi,EarthPeople,I killed %ld person",count);}@end//————————————————////  Mars.h//  IOS150624_ObjectiveC_真正的代理10////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import <Foundation/Foundation.h>#import "ReceiveReportDelegate.h"@interface Mars : NSObject <ReceiveReportDelegate>@end//————————————////  Mars.m//  IOS150624_ObjectiveC_真正的代理10////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import "Mars.h"@implementation Mars- (void)report:(NSInteger)count{    NSLog(@"Hi,Mars people,I killed %ld person",count);}@end//———————————————////  Dog.h//  IOS150624_ObjectiveC_真正的代理10////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import <Foundation/Foundation.h>#import "ReceiveReportDelegate.h"@interface Dog : NSObject@property (assign,nonatomic)id <ReceiveReportDelegate>delegate;- (void)bark;@end//—————————————————////  Dog.m//  IOS150624_ObjectiveC_真正的代理10////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import "Dog.h"@implementation Dog- (void)bark{    NSLog(@"汪");    [self.delegate report:arc4random()%10];}@end//————————————————////  main.m//  IOS150624_ObjectiveC_真正的代理10////  Created by PengJunlong on 15/6/24.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import <Foundation/Foundation.h>#import "Dog.h"#import "Person.h"#import "Mars.h"int main(int argc, const char * argv[]) {    @autoreleasepool {        Dog *dog = [[Dog alloc] init];        Person *xiaoZhang = [[Person alloc] init];        dog.delegate = xiaoZhang;        [dog bark];        //结果:        //汪        //Hi,EarthPeople,I killed 5 person                Person *xiaoMing = [[Person alloc] init];        dog.delegate = xiaoMing;        [dog bark];        //结果:        //汪        //Hi,EarthPeople,I killed 1 person    }    return 0;}


0 0
原创粉丝点击