移动网站开发教材,建立网站服务的公司网站,专业网站建设的公司排名,青岛软件制作1、正式协议的定义 protocol 协议名 父协议1, 父协议2{零个到多个方法定义} 一个协议可以有多个直接父协议#xff0c;但协议只能继承协议#xff0c;不能继承类
协议只有方法签名#xff0c;没有方法实现 2、实现协议
interface 类名 : 父类 协议1#xff0…1、正式协议的定义 protocol 协议名 父协议1, 父协议2{零个到多个方法定义} 一个协议可以有多个直接父协议但协议只能继承协议不能继承类
协议只有方法签名没有方法实现 2、实现协议
interface 类名 : 父类 协议1协议2…end 协议和java里面的接口差不多 如果要使用协议定义变量有如下两种语法
NSObject协议1协议2*变量
id协议1协议2 变量 optional关键字之后声明的方法可选实现
required关键字之后声明的方法必选实现 3、测试Demo
1)、FirstProtocol.h
#ifndef FirstProtocol_h
#define FirstProtocol_hprotocol FirstProtocol
-(void)first;
end#endif /* FirstProtocol_h */ 2)、SecondProtocol.h
#ifndef SecondProtocol_h
#define SecondProtocol_h
protocol SecondProtocol
-(void)second;
end#endif /* SecondProtocol_h */ 3)、ThirdProtocol.h
#import FirstProtocol.h
#import SecondProtocol.h#ifndef ThirdProtocol_h
#define ThirdProtocol_h
protocol ThirdProtocol FirstProtocol, SecondProtocol
-(void)third;
end#endif /* ThirdProtocol_h */ 4)、DoProtocol.h
#import Foundation/Foundation.h
#import ThirdProtocol.h#ifndef DoProtocol_h
#define DoProtocol_h
interface DoProtocol : NSObject ThirdProtocol
end#endif /* DoProtocol_h */ 5)、DoProtocol.m
#import Foundation/Foundation.h#import DoProtocol.himplementation DoProtocol
-(void)first
{NSLog(this first method);
}
-(void)second
{NSLog(this second method);
}
-(void)third
{NSLog(this third method);
}
end 6)、main.m
#import DoProtocol.h
#import ThirdProtocol.h
#import FirstProtocol.hint main(int argc, char * argv[]) {autoreleasepool {DoProtocol *protocal [DoProtocol new];[protocal first];[protocal second];[protocal third];NSObjectFirstProtocol *first [[DoProtocol alloc] init];[first first];idSecondProtocol second [[DoProtocol alloc] init];[second second];}
} 4、运行结果
this first method
this second method
this third method
this first method
this second method