The only way to do great work is to love what you do. If you haven’t found it yet, keep looking. Don’t settle. As with all matters of the heart, you’ll know when you find it.
研究一下OC和Swift的协议,今天先来研究OC的协议。
什么是协议:
协议是多个类共享一个方法列表,协议中列出的方法没有相应的实现,由使用者来实现。
协议是个什么样子:
@protocol 协议名 <父协议>
@required(默认值)
必须实现的方法
@optional
可选实现的方法
@end
OC中的协议就是这个样子,简单明了,你懂的!
如何使用协议:
直接在继承类(NSObject)后面 <协议名>即可,如果是多个协议,则用逗号分隔即可。举例如下:
@interface Car : NSObject <协议名>
@interface Car : NSObject <协议名1,协议名2>
协议的实现是在使用的类中完成的。