1.协议
1.1是一种要求,或是一种规则
1.2对应程序来讲,使只生成方法的声明,不实现
1.3协议必须被某个类采纳,且在该类中给出协议中方法的函数体
1.4对于采纳协议的类,可以和其他类一样使用
新建协议.png
TRMyProtocol.h
#import <Foundation/Foundation.h>
@protocol TRMyProtocol <NSObject>
@property NSString *content;
-(NSString *)show;
@end
TRMyClass.h
#import <Foundation/Foundation.h>
#import "TRMyProtocol.h"
@interface TRMyClass : NSObject<TRMyProtocol>
@end
TRMyClass.m
#import "TRMyClass.h"
@implementation TRMyClass
@synthesize content = _content;
-(NSString *)show
{
return [NSString stringWithFormat:@"秀一下,%@",self.content];
}
@end
TRProtocol1.h
#import <Foundation/Foundation.h>
@protocol TRProtocol1 <NSObject>
@optional//在采纳协议的类中,可实现可不实现
-(NSString *)method0;
@required//在采纳协议的类中,必须实现。默认的
-(NSString *)method1;
@end
TRProtocol2.h
#import <Foundation/Foundation.h>
#import"TRProtocol1.h"
@protocol TRProtocol2 <TRProtocol1>
-(NSString *)method2;
@end
TRClassA.h
#import <Foundation/Foundation.h>
#import "TRProtocol2.h"
@interface TRClassA : NSObject<TRProtocol2>
-(NSString *)method;
@end
TRClassA.m
import "TRClassA.h"
@implementation TRClassA
-(NSString *)method
{
return @"classA method";
}
-(NSString *)method0
{
return @"classA method0";
}
-(NSString *)method1
{
return @"classA method1";
}
-(NSString *)method2
{
return @"classA method2";
}
@end
TRProtocol3.h
#import <Foundation/Foundation.h>
@protocol TRProtocol3 <NSObject>
-(NSString *)method3;
@end
TRProtocol4.h
#import <Foundation/Foundation.h>
#import "TRProtocol2.h"
#import "TRProtocol3.h"
@protocol TRProtocol4 <TRProtocol2,TRProtocol3>
-(NSString *)method4;
@end
TRClassB.h
#import <Foundation/Foundation.h>
#import "TRProtocol4.h"
@interface TRClassB : NSObject<TRProtocol4>
@end
TRClassB.m
#import "TRClassB.h"
@implementation TRClassB
-(NSString *)method0
{
return @"ClassB method0";
}
-(NSString *)method1
{
return @"ClassB method1";
}
-(NSString *)method2
{
return @"ClassB method2";
}
-(NSString *)method3
{
return @"ClassB method3";
}
-(NSString *)method4
{
return @"ClassB method4";
}
@end
TRProtocol5.h
#import <Foundation/Foundation.h>
@protocol TRProtocol5 <NSObject>
-(NSString *)method5;
@end
TRClassC.h
#import <Foundation/Foundation.h>
#import "TRProtocol4.h"
#import "TRProtocol5.h"
@interface TRClassC : NSObject<TRProtocol4,TRProtocol5>
@end
TRClassC.m
#import "TRClassC.h"
@implementation TRClassC
-(NSString *)method0
{
return @"ClassC method0";
}
-(NSString *)method1
{
return @"ClassC method1";
}
-(NSString *)method2
{
return @"ClassC method2";
}
-(NSString *)method3
{
return @"ClassC method3";
}
-(NSString *)method4
{
return @"ClassC method4";
}
-(NSString *)method5
{
return @"ClassC method5";
}
ViewController.m
#import "ViewController.h"
#import "TRMyClass.h"
#import "TRClassA.h"
#import "TRClassB.h"
#import "TRClassC.h"
typedef enum
{
CLASSA,CLASSB,CLASSC,
}Type;
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *outputLabel;
-(void)method1;
-(void)method2;
-(void)method3;
-(void)method4;
-(void)method5;
-(void)method6;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self method6];
}
-(void)method1
{
TRMyClass *obj = [[TRMyClass alloc]init];
obj.content = @"协议中的属性";
self.outputLabel.text = [obj show];
id<TRMyProtocol> id1 =obj;//id1这个万能指针是一个受限的万能指针,他只能指向采纳了TRMyProtocol协议的类的对象
id1.content = @"万能指针使用属性";
self.outputLabel.text = [id1 show];
}
-(void)method2
{
TRClassA *obj = [[TRClassA alloc] init];
id<TRProtocol1>id1 = obj;
//[id1 method];//id1只能调用受限协议的方法
self.outputLabel.text = [id1 method0];
self.outputLabel.text = [id1 method1];
//self.outputLabel.text = [id1 method2];//id1不能调用TRProtocol2中的方法
id <TRProtocol2> id2 = obj;
//self.outputLabel.text = [id2 method];
self.outputLabel.text = [id2 method0];
self.outputLabel.text = [id2 method1];
self.outputLabel.text = [id2 method2];
}
-(void)method3
{
TRClassB *obj = [[TRClassB alloc] init];
id<TRProtocol1>id1 = obj;
self.outputLabel.text = [id1 method0];
self.outputLabel.text = [id1 method1];
id<TRProtocol2> id2 = obj;
self.outputLabel.text = [id2 method0];
self.outputLabel.text = [id2 method1];
self.outputLabel.text = [id2 method2];
id<TRProtocol3>id3 = obj;
self.outputLabel.text =[id3 method3];
id<TRProtocol4>id4 = obj;
self.outputLabel.text =[id4 method0];
self.outputLabel.text =[id4 method1];
self.outputLabel.text =[id4 method2];
self.outputLabel.text =[id4 method3];
self.outputLabel.text =[id4 method4];
}
-(void)method4
{
TRClassC *obj = [[TRClassC alloc] init];
id<TRProtocol4,TRProtocol5>id1 = obj;
self.outputLabel.text = [id1 method0];
self.outputLabel.text = [id1 method1];
self.outputLabel.text = [id1 method2];
self.outputLabel.text = [id1 method3];
self.outputLabel.text = [id1 method4];
self.outputLabel.text = [id1 method5];
}
-(void)show:(id<TRProtocol1>)a
{
self.outputLabel.text = [a method0];
}
-(void)method5
{
[self show:[[TRClassA alloc]init]];
[self show:[[TRClassB alloc]init]];
[self show:[[TRClassC alloc]init]];
}
-(id<TRProtocol1>)get:(Type)type
{
switch (type) {
case CLASSA:
return [[TRClassA alloc] init];
case CLASSB:
return [[TRClassB alloc] init];
case CLASSC:
return [[TRClassC alloc] init];
}
}
-(void)method6
{
self.outputLabel.text = [[self get:CLASSA] method0];
self.outputLabel.text = [[self get:CLASSB] method0];
self.outputLabel.text = [[self get:CLASSC] method0];
}
@end
USB协议练习
TRUSB.h//协议
#import <Foundation/Foundation.h>
@protocol TRUSB <NSObject>
@optional
-(NSString *)write:(int)bytes;
@required
-(NSString *)read:(int)bytes;
@end
TRUDisk.h
#import <Foundation/Foundation.h>
#import "TRUSB.h"
@interface TRUDisk : NSObject<TRUSB>
@end
TRUDisk.m
#import "TRUDisk.h"
@implementation TRUDisk
-(NSString *)write:(int)bytes
{
return [NSString stringWithFormat:@"向U盘中写入%d个字节的数据",bytes];
}
-(NSString *)read:(int)bytes
{
return [NSString stringWithFormat:@"从U盘中读入%d个字节的数据",bytes];
}
@end
TRMoveDisk.h
#import <Foundation/Foundation.h>
#import "TRUSB.h"
@interface TRMoveDisk : NSObject<TRUSB>
@end
TRMoveDisk.m
#import "TRMoveDisk.h"
@implementation TRMoveDisk
-(NSString *)read:(int)bytes
{
return [NSString stringWithFormat:@"从移动硬盘中读入%d个字节的数据",bytes];
}
-(NSString *)write:(int)bytes
{
return [NSString stringWithFormat:@"向移动硬盘中写入%d个字节的数据",bytes];
}
@end
TRCamera.h
#import <Foundation/Foundation.h>
#import "TRUSB.h"
@interface TRCamera : NSObject<TRUSB>
@end
TRCamera.m
#import "TRCamera.h"
@implementation TRCamera
-(NSString *)read:(int)bytes
{
return [NSString stringWithFormat:@"从摄像头中读入%d个字节的数据",bytes];
}
@end
TRComputer.h
#import <Foundation/Foundation.h>
#import "TRUSB.h"
@interface TRComputer : NSObject
@property id<TRUSB>usb1;
@property id<TRUSB>usb2;
-(NSString *)dataCopyFromUsblToUsb2:(int)bytes;
@end
TRComputer.m
#import "TRComputer.h"
@implementation TRComputer
-(NSString *)dataCopyFromUsblToUsb2:(int)bytes
{
NSString *str = @"";
str = [self.usb1 read:bytes];
NSString *str1 = [self.usb2 write:bytes];
str = [str stringByAppendingFormat:@"\n%@",str1];
return str;
}
@end
ViewController.m
#import "ViewController.h"
#import "TRUDisk.h"
#import "TRMoveDisk.h"
#import "TRCamera.h"
#import "TRComputer.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *outputLabel;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
TRComputer *com = [[TRComputer alloc] init];
TRUDisk *uDisk = [[TRUDisk alloc]init];
com.usb1 = uDisk;
TRMoveDisk *md = [[TRMoveDisk alloc]init];
com.usb2 = md;
self.outputLabel.text = [com dataCopyFromUsblToUsb2:1024];
TRCamera *c = [[TRCamera alloc] init];
com.usb1 = c;
com.usb2 = uDisk;
self.outputLabel.text = [com dataCopyFromUsblToUsb2:1024];
}
@end
1.5协议可以被继承,包括多个父协议
1.6协议可以被多重采纳
1.7使用形式
1.7.1方法参数
1.7.2方法的返回值
网友评论