Object-c 协议@protocol(@required、@optional)

一、什么是协议?

1.协议声明了可以被任何类实现的方法
2.协议不是类,它是定义了一个其他对象可以实现的接口
3.如果在某个类中实现了协议中的某个方法,也就是这个类实现了那个协议。
4.协议经常用来实现委托对象。一个委托对象是一种用来协同或者代表其他对象的特殊对象。
5:委托,就是调用自己定义方法,别的类来实现。
另外:
可以用来声明一大堆方法(不能声明成员变量)
只要某个类遵守了这个协议,就相当于拥有这个协议中的所有方法声明
只要父类遵守了某个协议,就相当于子类也遵守了

二、格式
@protocol 协议名称
// 方法声明列表
@end

某个类遵守某个协议

@interface 类名 : 父类 <协议名称>
@end
某个协议遵守某个或者某些协议,一个协议可以遵守其他多个协议,
多个协议之间用逗号 , 隔开。
@protocol 协议名称 <其他协议名称1, 其他协议名称2>
 @end

 定义变量时,限制这个变量保存的对象遵守某个协议

类名<协议名称> *变量名;

id<协议名称> 变量名;
NSObject<MyProtocol> *obj;
id<MyProtocol> obj2;

 @property中声明的属性也可用做一个遵守协议的限制

@property (nonatomic, strong) 类名<协议名称> *属性名;
@property (nonatomic, strong) id<协议名称> 属性名;
@property (nonatomic, strong) Dog<MyProtocol> *dog;
@property (nonatomic, strong) id<MyProtocol> dog2;

三、协议里的关键字

协议中有2个关键字可以控制方法是否要实现(默认是@required),在大多数情况下,
用途在于程序员之间的交流
@required:这个方法必须要实现(若不实现,编译器会发出警告)
@required
- (void)test;
- (void)test2;

@optional:这个方法不一定要实现

@optional
- (void)test3;

四、基协议

NSObject是一个基类,最根本最基本的类,任何其他类最终都要继承它
其实还有一个协议,名字也叫NSObject,它是一个基协议,最根本最基本的协议
NSObject协议中声明很多最基本的方法,比如description、retain、release等
建议每个新的协议都要遵守NSObject协议

 
五、注意点:

协议可用定义在单独.h文件中,也可用定义在某个类中
1> 如果这个协议只用在某个类中,应该把协议定义在该类中
2> 如果这个协议用在很多类中,就应该定义在单独文件中
分类可用定义在单独.h和.m文件中,也可用定义在原来类中
一般情况下,都是定义在单独文件
定义在原来类中的分类,只要求能看懂语法

 

  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure! This is a common command used in Makefiles to generate dependency files during the compilation process in C/C++ programming. Here is an explanation of each parameter: - `-MD`: This parameter tells the compiler to generate dependency files in the Makefile format. These files are used to track dependencies between source files and header files. - `-MP`: This parameter generates a phony target for each header file dependency, which ensures that the Makefile won't fail if a header file is deleted or renamed. - `-MF $(depfile)`: This parameter specifies the name of the dependency file to be generated. The `$(depfile)` variable is typically defined earlier in the Makefile and contains the name and path of the dependency file. - `-MT $@`: This parameter specifies the name of the target to be built. The `$@` variable represents the name of the target being built, which is typically an object file. - `-c`: This parameter tells the compiler to generate an object file, but not to link it with other object files to create an executable. - `-o $@`: This parameter specifies the name of the object file to be generated. The `$@` variable represents the name of the target being built, which is typically an object file. - `$<`: This is a special variable that represents the name of the first prerequisite (dependency) of the target. In this case, it represents the name of the source file being compiled. Here is an example command: ``` gcc -MD -MP -MF main.d -MT main.o -c -o main.o main.c ``` This command compiles the source file "main.c" into an object file named "main.o". It also generates a dependency file named "main.d" that tracks the dependencies between "main.c" and any included header files. The `$(depfile)` and `$@` variables are typically defined earlier in the Makefile.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值