#import <Foundation/Foundation.h>
@protocol Study <NSObject>
默认就是@required
- (void)test3;
表示必须实现的方法
// 虽然字面上说是必须实现,但是编译器并不强求某个类进行实现
@required
- (void)test;
- (void)test1;
// @optional表示可选(可实现\也可不实现)
@optional
- (void)test2;
@end
main.m文件
#import <Foundation/Foundation.h>
#import "Student.h"
Study;
int main(int argc, const char
{
@autoreleasepool {
Student *stu = [[[Student alloc] init] autorelease];
// OC是弱语法的,对类型要求不严格
// NSString *stu = [[[Student alloc] init] autorelease];
// [stu stringByAbbreviatingWithTildeInPath];
// conformsToProtocol:判断是否遵守了某个协议
if ([stu conformsToProtocol:@protocol(Study)]) {
NSLog(@"Student遵守了Study这个协议");
}
// respondsToSelector:判断是否实现了某个方法
if ( ![stu respondsToSelector:@selector(test)] ) {
NSLog(@"Student没有实现test这个方法");
}
}
return 0;
}
OC之【@protocol协议】
原创wx6392bc05d177b 博主文章分类:【IOS移动端】 ©著作权
文章标签 ios开发 objective-c #import 编译器 文章分类 运维
©著作权归作者所有:来自51CTO博客作者wx6392bc05d177b的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
linux之TCP协议
linux下TCP协议的认识,包括确认应答,超时重传,流量控制,滑动窗口,拥塞控制,三次握手四次挥手等。
TCP 面向字节流 可靠性 有连接 滑动窗口 -
网络协议之:memcached binary protocol详解
简介 前面讲到了memcached的文本协议,虽然文本协议看起来非常简单,但是对于客户端来说一般还是会选择效率更高的二
java 程序那些事 memcached 数据 服务器端