首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Objective-C 的正式协媾和非正式协议

2012-09-17 
Objective-C 的正式协议和非正式协议@protocol myprotocol NSObject@optional-(void)print:(int)value/

Objective-C 的正式协议和非正式协议

@protocol myprotocol <NSObject>@optional-(void)print:(int)value;//可选的方法@required-(int)printValue:(int)value1 andValue:(int)value2;//必须实现的@end#import <Foundation/Foundation.h>#import "myprotocol.h"//实现协议 myprotocol@interface mytest : NSObject<myprotocol> {}- (void)showInfo;@end#import "mytest.h"@implementation mytest-(void)showInfo{NSLog(@"I am in showInfo");}//实现协议必须实现的-(int)printValue:(int)value1 andValue:(int)value2{NSLog(@"print value1 %d,value2 %d",value1,value2);return 0;}//实现可选的-(void)print:(int)value{NSLog(@"print value is %d",value);}@end#import <Foundation/Foundation.h>#import "mytest.h"#import "myprotocol.h"int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; // insert code here... NSLog(@"Hello, World!");mytest *test=[[mytest alloc]init];[test showInfo];[test printValue:20 andValue:30];//print协议是可选的,所以在用之前一定要判断是否实现了,不然可能会出错,使用下面的方法//[test print:20];SEL sel=@selector(print:);if([test respondsToSelector:sel]){[test print:11];}//用协议的方式实现id<myprotocol> protocol =[[[mytest alloc]init]autorelease];[protocol showInfo];[protocol printValue:200 andValue:300];if([protocol respondsToSelector:@selector(print:)]){[protocol print:111];}[test release]; [pool drain]; return 0;}#import <Foundation/Foundation.h>@protocol dogBark;@interface Dog : NSObject {int _ID;NSTimer *timer;int barkCount;id <dogBark> delegate;//存放狗的主人}@property int ID;@property (assign)id <dogBark> delegate;@end//定义一个人和狗通讯的协议 protocol@protocol dogBark<NSObject>-(void)bark:(Dog*)thisDog count:(int)count;@end#import "Dog.h"@implementation Dog@synthesize ID=_ID;@synthesize delegate;-(id)init{if(self = [super init]){//创建一个定时器user,每隔1.0s 就调用updateTimer:nil,并传递一个参数niltimer=[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES];}return self;}-(void) updateTimer:(id)arg { barkCount++; NSLog(@"dog bar %d",barkCount); //调用主人delegate的bark:count方法, [delegate bark:self count:barkCount]; //回调机制 }@end#import <Foundation/Foundation.h>#import "Dog.h"@interface Person : NSObject<dogBark>{Dog *_dog;}@property (retain) Dog *dog;@end#import "Person.h"@implementation Person@synthesize dog=_dog;-(void)setDog:(Dog*)aDog{if(_dog!=aDog){[_dog release];_dog=[aDog retain];// 通知dog的主人是当前人,self[_dog setDelegate:self];}}//当狗叫的时候,让狗来调用人的方法//这个方法来源于dogBark协议,Person类来实现-(void)bark:(Dog*)thisDog count:(int)count{NSLog(@"Person bark: this dog %d bark %d",[thisDog ID],count);}-(void)dealloc{self.dog=nil;[super dealloc];}@end#import <Foundation/Foundation.h>#import "Dog.h"#import "Person.h"int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; // insert code here... NSLog(@"Hello, World!");Person *xiaoli = [[Person alloc]init];Dog *dog=[[Dog alloc]init];[dog setID:10];[xiaoli setDog:dog];[dog release];//程序循环在这里while (1) {[[NSRunLoop currentRunLoop]run];}[xiaoli release]; [pool drain]; return 0;}#import <Cocoa/Cocoa.h>@interface dog : NSObject {int _ID;}@property int ID;@end#import "dog.h"@implementation dog@synthesize ID=_ID;-(id)init{self=[super init];return self;}@end#import <Cocoa/Cocoa.h>#import "dog.h"@interface person : NSObject {dog *_mydog;}-(void)setDog:(dog*)aDog;-(id)mydog;-(void)callFun;@end#import "person.h"#import "nsobject_categroy.h"@implementation person-(void)setDog:(dog*)aDog{if (_mydog!=aDog) {[_mydog release];_mydog=[aDog retain];}}-(id)mydog{return _mydog;}-(void)callFun{NSLog(@"call Fun!");[_mydog callFromNSObject];}-(void)dealloc{[self setDog:nil];[super dealloc];}@end#import "nsobject_categroy.h"@implementation NSObject(myCategroy) -(void)callFromNSObject{NSLog(@"I AM NSOBJECT FUNCTIONS");}@end#import <Foundation/Foundation.h>#import "person.h"#import "dog.h"int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; // insert code here... NSLog(@"Hello, World!");dog *d=[[dog alloc]init];[d setID:10];person *p=[[person alloc]init];[p setDog:d];[p callFun];[p release]; [pool drain]; return 0;}这样就会调用callFromNSObject方法


类别主要有三个功能:

一、利用类别分散实现

二、利用类别创建前向引用,可以实现私有函数

三、非正式协议和委托类别

热点排行