/**
* 生活中的协议
* 租户和房东之间的协议
*
* 房东的义务:a.交水电费 b.交暖气费 c.交业务费
* 租户的义务:a.交房租 b.保护房内物品
*
*/
#import <Foundation/Foundation.h>
/**
* 协议最基本用法
* @protocol 协议名 <协议类型>
*
* 在协议中只声明方法,而没有方法实现
*/
@protocol USB <NSObject>
//有@required修饰 必须实现的方法
@required
- (void)readData;
- (void)writeData;
//@optional修饰 系统默认 可实现 可不实现
@optional
- (void)showData;
@end
/**
* 定义一个Computer类 遵循 USB 协议
*/
@interface Computer : NSObject<USB>
@end
@implementation Computer
- (void)readData {
NSLog(@"读数据");
}
- (void)writeData {
NSLog(@"写数据");
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Computer *computer = [Computer new];
[computer readData];
[computer writeData];
}
return 0;
}
Objective-C 协议最基本解释
最新推荐文章于 2022-10-04 09:28:24 发布