oc 协议
来源:互联网 发布:stl源码剖析 怎么样 编辑:程序博客网 时间:2024/05/29 04:09
@protocol myProtocol <NSObject> // 基协议
@required// 声明了必须要实现的,默认情况下都是
@required
- (void)walk;
- (void)speak;
- (void)think;// think在类实现中未实现会警告!!!
@optional
- (void)sing;
- (void)laugh;
@end
协议可以声明一大堆方法,但不能声明成员变量;
两个协议之间不能继承,但协议可以遵守另一个协议;
子类可以遵守父类遵守的协议;
NSObject<myProtocol,allProtocol> * stu = [[Studentalloc]init];
等价于
Student *stu = [[Student alloc]init];// 任何类
[stu eat];// 子类重写了,调用子类的
[stu run];// 子类没重写,向上找找到父类的方法
student can eat
person can run
Program ended with exit code: 0
Student * stu = [[Student alloc]init];
Student <myProtocol> * student = stu;
NSObject<myProtocol> * student = stu;
id<myProtocol> student = stu;
若报错,就检验出stu没遵守协议
#import "Student.h"
@implementation Student
- (void)exam{
NSLog(@"student can exam.");
}
- (void)eat{
NSLog(@"student can eat");
}
@end
#import <Foundation/Foundation.h>
@protocol allProtocol <NSObject>
- (void)eat;
- (void)run;
@end
#import "Person.h"
@implementation Person
- (void)eat{
NSLog(@"person can eat");
}
- (void)run{
NSLog(@"person can run");
}
@end
- OC协议
- oc 协议
- oc 协议
- OC协议
- OC 协议
- oc ------协议
- oc协议
- OC-协议
- OC协议
- OC-协议
- OC学习:OC中的协议
- OC之【@protocol协议】
- 7、协议protocol-OC
- 7、OC Protocol协议
- OC中的协议
- OC协议 委托
- oc协议,延展,分类
- OC--block,@protocol协议
- 阿萨
- 网关,路由器的区别
- hihoCoder_推箱子
- 地方
- 获取手机屏幕宽度和高度
- oc 协议
- shell基础
- 是非得失
- Mac中权限相关操作
- Java中堆内存和栈内存详解
- 研究下skynet,云风大神的开源框架
- 自定义ProgressBar
- vim 高级使用技巧第二篇
- java 多线程下载文件