当前位置:网站首页>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)];
边栏推荐
- MySQL查询为啥慢了?
- 如何为Web3.0世界启动完美的DAO
- Chapter7 : Network-Driven Drug Discovery
- 多个平台显示IP属地,必须大力推行互联网实名制
- JdbcTemplate概述和测试
- 打卡第 2 天: urllib简记
- DSPE-PEG-Aldehyde, DSPE-PEG-CHO, Phospholipid-Polyethylene Glycol-Aldehyde A hydrophobic 18-carbon phospholipid
- Qiangwang Cup 2022 - WEB
- 未知点云结构文件转换需求
- ue unreal 虚幻 高分辨率无缩放 编辑器字太小 调整编辑器整体缩放
猜你喜欢
docker 部署redis集群
Spss-一元回归实操
Exploration and Practice of Database Governance
UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xd6 in position 120: invalid continuation byte
硬件开发定制全流程解析
SPSS-unary regression practice
Chapter7 : Network-Driven Drug Discovery
ES6高级-async的用法
数电快速入门(四)(组合逻辑电路的分析以及设计的介绍)
Win11如何开启Telnet客户端?
随机推荐
Codeforces Round #811 (Div. 3)
DSPE-PEG-Aldehyde,DSPE-PEG-CHO,磷脂-聚乙二醇-醛基一种疏水18碳磷脂
模拟对抗之红队免杀开发实践
国内的PMP证书含金量到底如何
Altium Designer 19.1.18 - Protecting Locked Objects
27.降维
moke、动态图片资源打包显示
Exploration and Practice of Database Governance
Flutter 实现背景图片毛玻璃效果
Chapter7 : Network-Driven Drug Discovery
可视化工作流引擎开发OA系统,让企业少花冤枉钱
硬件开发定制全流程解析
STM32MP157A驱动开发 | 01- 板载LED作为系统心跳指示灯
unity2D横版游戏教程9-对话框dialog
Named routes, the role of name in components
Android 面试——如何写一个又好又快的日志库?
数电快速入门(一)(BCD码和三种基本逻辑运算的介绍)
Spss-系统聚类软件实操
1319_STM32F103串口BootLoader移植
PowerCLi import license to vCenter 7