Objective-C 协议(接口)

时间:2023-03-09 15:34:40
Objective-C 协议(接口)

Objective-C 协议类似于java语言中的接口

新建文件步骤:Objective-C File ---> File Type = "Protocol"

@protocol

 // 协议 相当于C++语言中的接口
@protocol IPeople <NSObject>
-(int)getAge;
-(NSString*)getName;
@end // 类定义 并实现IPeople接口 多个接口用,相隔
@interface Man : NSObject<IPeople>
-(int)getAge;
-(NSString*)getName;
@end // 类实现
@implementation Man
-(int)getAge{
return ;
} -(NSString*)getName{
return @"jinpangpang";
}
@end // 调用
int main(int argc, char * argv[]) {
Man *m = [[Man alloc] init];
NSLog(@"%@",[m getName]);
NSLog(@"%d",[m getAge]);
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}