Objective-C 协议(接口)

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

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

@protocol

 1 // 协议 相当于C++语言中的接口
 2 @protocol IPeople <NSObject>
 3 -(int)getAge;
 4 -(NSString*)getName;
 5 @end
 6 
 7 // 类定义  并实现IPeople接口  多个接口用,相隔
 8 @interface Man : NSObject<IPeople>
 9 -(int)getAge;
10 -(NSString*)getName;
11 @end
12 
13 // 类实现
14 @implementation Man
15 -(int)getAge{
16     return 20;
17 }
18 
19 -(NSString*)getName{
20     return @"jinpangpang";
21 }
22 @end
23 
24 
25 // 调用
26 int main(int argc, char * argv[]) {
27     Man *m  = [[Man alloc] init];
28     NSLog(@"%@",[m getName]);
29     NSLog(@"%d",[m getAge]);
30     @autoreleasepool {
31         return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
32     }
33 }
原文地址:https://www.cnblogs.com/-jpp/p/5002955.html