OC6_代理的基本概念
// // Person.h // OC6_代理的基本概念 // // Created by zhangxueming on 15/6/24. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import <Foundation/Foundation.h> #import "Dog.h" @interface Person : NSObject { Dog *_delegate; } @property (nonatomic)Dog *delegate; - (void)go; @end // // Person.m // OC6_代理的基本概念 // // Created by zhangxueming on 15/6/24. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import "Person.h" @implementation Person - (void)go { [_delegate bark]; } @end
// // Dog.h // OC6_代理的基本概念 // // Created by zhangxueming on 15/6/24. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import <Foundation/Foundation.h> @interface Dog : NSObject - (void)bark; @end // // Dog.m // OC6_代理的基本概念 // // Created by zhangxueming on 15/6/24. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import "Dog.h" @implementation Dog - (void)bark { NSLog(@"Wang wang wang ..."); } @end
// // main.m // OC6_代理的基本概念 // // Created by zhangxueming on 15/6/24. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import <Foundation/Foundation.h> #import "Person.h" #import "Dog.h" //代理: //买火车票 你 --> 黄牛 //张 <--> 律师 //以张的角度 律师是张的代理 (正向代理) //从律师的角度看 张是律师的代理 (反向代理) int main(int argc, const char * argv[]) { @autoreleasepool { Person *xiaoXin = [[Person alloc] init]; Dog *dog = [[Dog alloc] init]; xiaoXin.delegate = dog; [xiaoXin go]; } return 0; }