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
- 20150624_OC之代理及反向代理的简单例子
- Nginx反向代理的应用例子及简单的笔记
- nginx简单的反向代理
- nginx反向代理例子
- 反向代理原理,及反向代理开放的软件应用
- Nginx反向代理的配置两个例子
- 正向代理、反向代理及透明代理
- ubuntu之apache正向代理及反向代理(ProxyPass\ProxyPassReverse)
- ubuntu之apache正向代理及反向代理(ProxyPass\ProxyPassReverse)
- ubuntu之apache正向代理及反向代理(ProxyPass/ProxyPassReverse)
- 代理模式的简单例子
- 正向代理和反向代理的简单理解
- 配置简单的nginx反向代理
- Nginx作为反向代理的简单应用
- nginx配置简单的反向代理
- 最简单的nginx反向代理tomcat
- 如何简单理解ngnix的反向代理
- Nginx反向代理的简单配置应用
- android 数据库sqlite的使用方法
- LeetCode之Regular Expression Matching
- c/c++内存机制
- android关于下拉刷新列表的使用列表
- Android实践-使用ViewPager和Fragment实现带Tab导航条的仿新闻类页面
- 20150624_OC之代理及反向代理的简单例子
- mysql 5.6 时间函数的变化 FROM_UNIXTIME CURRENT_TIMESTAMP
- .NET 获取类内成员的名称,以字符串形式显示
- js取得table中td值的问题
- java修饰符
- 黑马程序员——Java基础--集合(三)
- 支付宝高级技术专家杨传辉:OceanBase 0.4 从API到SQL
- C++ 不输入回车就不结束输入的实现方法
- 九度oj 题目1108:堆栈的使用