当前位置:网站首页>OC-协议
OC-协议
2022-08-04 21:37:00 【彭同学她同桌】
什么是协议
定义一组方法 由其他类来实现。
如果一个类实现了这个协议则代表这个类遵循了这个协议
格式
@protocol 协议的名字<父协议> // 一般情况下 父协议是NSObject
创建协议
//Pro.h
@protocol pro<NSObject>
-(void)play;//并不在Son的头文件中声明 而是在协议中声明这个方法
@optional//标识下面这个函数可实现 也可以不实现
-(void)read;
@required//标识下面这个函数必须实现 默认就是required
-(void)eat;
@end
导入协议
//Son.h
import"Pro.h"
@interface Son:NSObject<pro/*,这里可以添加其他的协议*/>
//-(void)play//不声明
@end
//Son.m
import"Son.h"
@implementation Son
-(void)play
{
NSLog(@"%@",__func__)
}
@end
//main.m
import"Foundation/Foundation.h"
import"Son.h"
int main()
{
Son *s = [[Son alloc]init];
[s play];//发现也能调用
}
协议限制传入的参数 也可以限制成员变量
创建协议
//people.h 创建一个人的协议
@protocol people<NSObject>
-(char*)name;
-(id)initWithName;
@end
创建女人类
//Woman.h
import"people.h"
@interface Woman:NSObject<people>
{
char *_name;
id friend;//可以改成下面这样
}
//@property id<people>friend;//直接限制成员变量
-(void)setWithFriend:(id)<people>f
@end
//Woman.m
import"Woman.h"
@implementation Woman
-(void)setWithFriend:(id)<people>f//只允许与遵守人协议的对象做朋友
{
NSLog(@"%@:%@",__func__,[f name]);
}
-(id)initWithName:(char*)n;
{
if((self = [[super alloc]init])!= nil)
{
_name = n;
}
return self;
}
-(char*)name
{
return name;
}
@end
创建男人类
//Man.h
import"people.h"
@interface Man:NSObject<people>
{
char *_name;
id *friend;
}
@end
//Man.m
import"Man.h"
@implementation Man
-(id)initWithName:(char*)n;
{
if((self = [[super alloc]init])!= nil)
{
_name = n;
}
return self;
}
-(char*)name
{
return name;
}
@end
创建狗类
//Dog.h
@interface Dog:NSObject
@end
//Dog.m
import"Dog.h"
@implementation Dog
@end
main
int main()
{
Woman *hong = [[Woman alloc]initWithName:@"小红"];
Man*ming = [[Man alloc]initWithName:@"小明"];
Dog*huang = [[Dog alloc]init];
[hong setWithFriend:ming];//setWithFriend:小明
//[hong setWithFriend:huang];//这样就会报错 因为dog不遵守人的协议
}
BOOL conformsToProtocol: 判断是否遵循协议
[hong conformsToProtocol:@protocol(people)];
边栏推荐
- 拼多多开放平台订单信息查询接口【pdd.order.basic.list.get订单基础信息列表查询接口(根据成交时间)】代码对接教程
- ue unreal 虚幻 高分辨率无缩放 编辑器字太小 调整编辑器整体缩放
- PowerCLi 批量配置NTP
- In which industries is the PMP certificate useful?
- OD-Model【6】:YOLOv2
- DSPE-PEG-Aldehyde,DSPE-PEG-CHO,磷脂-聚乙二醇-醛基一种疏水18碳磷脂
- PRIMAL: Pathfinding via Reinforcement and Imitation Multi-Agent Learning Code Analysis
- 看看XDOC如何做Word文档预览
- Unknown point cloud structure file conversion requirements
- LocalDate时间日期包的用法
猜你喜欢
ES6高级-async的用法
Arduino 电机测速
Develop your own text recognition application with Tesseract
如何将二叉搜索树转化为一个有序的双向链表(原树上修改)
PRIMAL: Pathfinding via Reinforcement and Imitation Multi-Agent Learning 代码解析
SPSS-unary regression practice
stm32mp157系统移植 | 移植ST官方5.10内核到小熊派开发板
模拟对抗之红队免杀开发实践
Zynq Fpga图像处理之AXI接口应用——axi_lite接口使用
[Linear Algebra 03] Elimination method display and 4 solutions of AX=b
随机推荐
LocalDate时间日期包的用法
看看XDOC如何做Word文档预览
Spss-系统聚类软件实操
js数据类型、节流/防抖、点击事件委派优化、过渡动画
What does Xinchuang mean?Which industries are involved?Why develop Xinchuang?
stm32mp157系统移植 | 移植ST官方5.10内核到小熊派开发板
milvus配置相关
deepstream多相机显示布局
用Tesseract开发一个你自己的文字识别应用
webmine网页挖矿木马分析与处置
boostrap多选PID查找端口 window
基于 Milvus 和 ResNet50 的图像搜索(部署及应用)
未知点云结构文件转换需求
UDP通信
In which industries is the PMP certificate useful?
Codeforces Round #811 (Div. 3)
Red team kill-free development practice of simulated confrontation
【分布式】分布式ID生成策略
moke、动态图片资源打包显示
命名路由、组件中name的作用