OC8_代理基本概念
2015-06-24 20:18
323 查看
// // ProtectedDelegate.h // OC8_代理基本概念 // // Created by zhangxueming on 15/6/24. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import <Foundation/Foundation.h> @protocol ProtectedDelegate <NSObject> - (void)bark; @end // // Person.h // OC8_代理基本概念 // // Created by zhangxueming on 15/6/24. // Copyright (c) 2015年 zhangxueming. 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 // OC8_代理基本概念 // // Created by zhangxueming on 15/6/24. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import "Person.h" @implementation Person - (void)go { [_delegate bark]; } @end // // Cat.h // OC8_代理基本概念 // // Created by zhangxueming on 15/6/24. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import <Foundation/Foundation.h> #import "ProtectedDelegate.h"//包含头文件 @interface Cat : NSObject <ProtectedDelegate>//遵守这个协议 @end // // Cat.m // OC8_代理基本概念 // // Created by zhangxueming on 15/6/24. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import "Cat.h" @implementation Cat - (void)bark { NSLog(@"Miao miao miao ..."); } @end // // Dog.h // OC8_代理基本概念 // // Created by zhangxueming on 15/6/24. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import <Foundation/Foundation.h> #import "ProtectedDelegate.h" @interface Dog : NSObject <ProtectedDelegate> @end // // Dog.m // OC8_代理基本概念 // // 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 // OC8_代理基本概念 // // Created by zhangxueming on 15/6/24. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #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]; Dog *dog = [[Dog alloc] init]; xiaoXin.delegate = dog; [xiaoXin go]; Cat *cat = [[Cat alloc] init]; xiaoXin.delegate = cat; [xiaoXin go]; } return 0; }
相关文章推荐
- nginx location配置
- Activity四种启动方式
- Objective-C与Javascript相互通信的中间件
- 让程序员跳槽的非钱原因
- Struts2数据封装机制
- 入行几年后,互联网的未来应该在哪里?
- 《构建之法》13~17章
- 实验五 操作系统之存储管理
- linux搭建LAMP架构服务
- java web项目的访问路径
- JAVA多线程---1基本概念
- 共享内存的使用
- 微信自定义菜单中文乱码问题
- 千分位设置
- weka up-sampling & down-sampling
- Factorial Trailing Zeroes
- 浅谈大型web系统架构
- 《数据结构与算法分析c++描述》读书笔记二——二叉树
- CentOS 7: 设置时区和时间
- c#, extract number from string