OC-protocol

 from : http://www.cnblogs.com/wendingding/p/3709604.html

1.Protocol:就一个用途,用来声明一大堆的方法(不能声明成员变量),不能写实现。

2.只要某个类遵守了这个协议,就拥有了这个协议中的所有方法声明。

3.只要父类遵守了某个协议,那么子类也遵守。

4.Protocol声明的方法可以让任何类去实现,protocol就是协议。

5.OC不能继承多个类(单继承)但是能够遵守多个协议。继承(:),遵守协议(< >)

6.基协议:<NSObject>是基协议,是最根本最基本的协议,其中声明了很多最基本的方法。

7.协议可以遵守协议,一个协议遵守了另一个协议,就可以拥有另一份协议中的方法声明。

 

 1 //
 2 //  MyProtocol.h
 3 //  WDDDelegateTest
 4 //
 5 //  Created by LiuChanghong on 15/9/26.
 6 //  Copyright © 2015年 LiuChanghong. All rights reserved.
 7 //
 8 
 9 #import <Foundation/Foundation.h>
10 
11 //MyProtocol协议,这个协议遵守NSObject基协议
12 @protocol MyProtocol <NSObject>
13 //在协议中可以声明很多有用的方法,但不能声明变量,也不能实现方法
14 @required
15 //required 必须要求实现
16 -(void)myName;
17 -(void)myAge;
18 
19 @optional
20 //optional 可选
21 -(void)myMomsName;
22 
23 
24 @end
MyProtocol.h
 1 //
 2 //  MyProtocol2.h
 3 //  WDDDelegateTest
 4 //
 5 //  Created by LiuChanghong on 15/9/26.
 6 //  Copyright © 2015年 LiuChanghong. All rights reserved.
 7 //
 8 
 9 #import <Foundation/Foundation.h>
10 
11 @protocol MyProtocol2 <NSObject>
12 
13 @required
14 
15 -(void)studentName;
16 -(void)studentAge;
17 
18 @end
MyProtocol2.h

 

 1 //
 2 //  Person.h
 3 //  WDDDelegateTest
 4 //
 5 //  Created by LiuChanghong on 15/9/26.
 6 //  Copyright © 2015年 LiuChanghong. All rights reserved.
 7 //
 8 
 9 #import <Foundation/Foundation.h>
10 #import "MyProtocol.h"
11 
12 @interface Person : NSObject<MyProtocol>//遵守协议
13 
14 @property (nonatomic,strong)NSString *personName;
15 
16 @end
Person.h
 1 //
 2 //  Person.m
 3 //  WDDDelegateTest
 4 //
 5 //  Created by LiuChanghong on 15/9/26.
 6 //  Copyright © 2015年 LiuChanghong. All rights reserved.
 7 //
 8 
 9 #import "Person.h"
10 
11 @implementation Person
12 
13 //required
14 -(void)myAge{
15     NSLog(@"My age is 18 .");
16 }
17 
18 -(void)myName{
19     NSLog(@"My name is Mike");
20 }
21 
22 //optional
23 -(void)myMomsName{
24     NSLog(@"可选的My mom's name is Lucy .");
25 }
26 
27 @end
Person.m

 

 1 //
 2 //  Student.h
 3 //  WDDDelegateTest
 4 //
 5 //  Created by LiuChanghong on 15/9/26.
 6 //  Copyright © 2015年 LiuChanghong. All rights reserved.
 7 //
 8 
 9 #import <Foundation/Foundation.h>
10 #import "MyProtocol2.h"
11 #import "MyProtocol.h"
12 
13 @interface Student : NSObject<MyProtocol2,MyProtocol>
14 
15 @end
Student.h
 1 //
 2 //  Student.m
 3 //  WDDDelegateTest
 4 //
 5 //  Created by LiuChanghong on 15/9/26.
 6 //  Copyright © 2015年 LiuChanghong. All rights reserved.
 7 //
 8 
 9 #import "Student.h"
10 
11 @implementation Student
12 
13 -(void)studentName{
14     NSLog(@"Student's name is Mary .");
15 }
16 
17 -(void)studentAge{
18     NSLog(@"Student's age is 14 .");
19 }
20 
21 -(void)myAge{
22     NSLog(@"My age is 20 .");
23 }
24 
25 -(void)myName{
26     NSLog(@"My name is Lucky .");
27 }
28 
29 @end
Student.m

 

 1 //
 2 //  main.m
 3 //  WDDDelegateTest
 4 //
 5 //  Created by LiuChanghong on 15/9/26.
 6 //  Copyright © 2015年 LiuChanghong. All rights reserved.
 7 //
 8 
 9 #import <Foundation/Foundation.h>
10 #import "Person.h"
11 #import "Student.h"
12 
13 int main(int argc, const char * argv[]) {
14     @autoreleasepool {
15         
16         Person *person = [Person new];
17         
18         [person myName];
19         [person myAge];
20         [person myMomsName];
21         
22         Student *student = [Student new];
23         [student myName];
24         [student myAge];
25         [student studentAge];
26         [student studentName];
27 
28     }
29     return 0;
30 }
测试

 

输出结果

 

1.协议的定义

@protocol 协议名称 <NSObject>

//方法声明列表

@end;

2.如何遵守协议

(1)类遵守协议

@protocol 类名:父类名 <协议名称1,协议名称2>

@end

(2)协议遵守协议

@protocol 协议名称 <其他协议名称>

@end;

3.协议方法声明中的关键字

(1)required (默认)要求实现,若没有实现则警告但不报错

(2)Optional 不要求实现

4.定义变量时遵守协议的限制

类名<协议名称> *变量名    NSObject<.Myprotocol> *obj;

Id  <协议名称>  变量名   id  <.Myprotocol> obj1;

5.Property中声明的属性也可以做遵守协议的限制

@property (nonatomic ,strong ) 类名<协议名称> *属性名;

@property (nonatomic ,strong ) id<协议名称>  属性名;

6.补充知识:协议本身写在.h头文件中,但也可以定义在任何地方。当这个协议只有这个类使用遵守时,一般把协议写在这个类里边,当这个协议需要多个类去实现时,就写在外边单独的文件中。

 

posted @ 2015-09-26 18:43  Geeok  阅读(305)  评论(0编辑  收藏  举报