程式如下:
協定部分:
//定義了一個拍賣的代理協定,裡面有四個參數,分别是物主,拍賣物的名字,物主提供的價格,拍賣物年代
#import <Foundation/Foundation.h>
@protocol auctionDelegate <NSObject>
-(void)owner:(id)aowner
name:(id)aname
price:(int)aprice
times:(id)atimes;
@end
主人類:
<.h檔案>
#import <Foundation/Foundation.h>
#import "auctionDelegate.h"
@class Company;
@interface Host : NSObject
{
NSString * _name;
NSString *_owner;
int _price;
NSString * _times;
BOOL _jewellery;//是否有寶貝
id<auctionDelegate> _delegate;//聲明了一個代理
}
@property(nonatomic,assign)id<auctionDelegate>delegate;
@property(nonatomic,retain)NSString * name;
@property(nonatomic,retain)NSString*owner;
@property(nonatomic,assign)int price;
@property(nonatomic,retain)NSString * times;
@property(nonatomic,assign)BOOL jewellery;
-(id)initWithName:(NSString *)aName
Owner:(NSString *)aOwner
Price:(int)aPrice
Times:(NSString *)aTimes
Delegate:(id<auctionDelegate>)aDelegate
Jewellery:(BOOL)aJewellery;
-(void)adelegate;//協定傳參執行個體方法
-(void)getinfo;//收到通知的反應
-(void)answer1:(Company *)c;//電話回複1
-(void)answer2;//電話回複2
@end
<.m>
#import "Host.h"
//定義了一個延展,裡面含有一個私有方法
#import "Company.h"
@interface Host()
-(BOOL)treasure;
@end
@implementation Host
@synthesize delegate=_delegate;
@synthesize name=_name;
@synthesize owner=_owner;
@synthesize price=_price;
@synthesize times=_times;
@synthesize jewellery=_jewellery;
-(id)initWithName:(NSString *)aName
Owner:(NSString *)aOwner
Price:(int)aPrice
Times:(NSString *)aTimes
Delegate:(id<auctionDelegate>)aDelegate
Jewellery:(BOOL)aJewellery;
{
if(self=[super init])
{
self.name=aName;
self.owner=aOwner;
self.price=aPrice;
self.times=aTimes;
self.delegate=aDelegate;
self.jewellery=aJewellery;
//注冊通知
NSNotificationCenter * center=[NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(getinfo) name:@"auctionResult" object:nil];
}
return self;}
//如果有寶貝傳回1,沒有傳回0
-(BOOL)treasure
{
BOOL value=0;
if(self.jewellery==1)
{
value=1;
}
return value;
}
//傳參,如果有寶貝,才傳參,沒有則無法進行拍賣
-(void)adelegate
{
if([self treasure])
{
[self.delegate owner:self.owner name:self.name price:self.price times:self.times];
}
else
{
NSLog(@"沒有寶貝提供拍賣");
}
}
//物主收到消息後的反應
-(void)getinfo
{
NSLog(@"%@:我已經知道了",self.owner);
}
//電話回複1
-(void)answer1:(Company *)c
{
NSLog(@"繼續拍賣!");
[self.delegate owner:self.owner name:self.name price:c.money times:self.times];
}
//電話回複2
-(void)answer2
{
NSLog(@"我不拍賣了!");
}
//釋放記憶體
-(void)dealloc
{
NSNotificationCenter * center=[NSNotificationCenter defaultCenter];
[center removeObserver:self name:@"auctionResult" object:nil];
[_name release];
[_owner release];
[_times release];
[super dealloc];
}
@end
公司類:
<.h>
#import <Foundation/Foundation.h>
#import "auctionDelegate.h"
@class Host;
@interface Company : NSObject<auctionDelegate>
{
//拍賣會現場提供的報價
int _money;
}
@property(nonatomic,assign)int money;
//在現場供價後,打電話給物主,是否繼續進行拍賣
-(void)phone1:(Host *)h;
-(id)initWithMoney:(int)aMoney;
@end
;
<.m>
#import "Company.h"
#import "Host.h"
@implementation Company
@synthesize money=_money;
-(id)initWithMoney:(int)aMoney;
{
if(self =[super init])
{
self.money=aMoney;
}
return self;}
-(void)phone
{
//拍賣成功後,給物主發送通知
NSNotificationCenter * center=[NSNotificationCenter defaultCenter];
[center postNotificationName:@"auctionResult" object:self];
}
//實作協定方法
-(void)owner:(id)aowner name:(id)aname price:(int)aprice times:(id)atimes
{
NSLog(@"物主:%@,寶物名稱:%@,價錢:%d,年代:%@",aowner,aname,aprice,atimes);
[self phone];
}
//正常拍賣,價格不可能和物主提供的價格一樣,是以拍賣方進行了比較人性化的處理,打電話給物主
//如果價格相等,繼續拍賣;如果大于物主價格,調用物主的第一通回複電話,如果小于,調用物主的第二桶電話
-(void)phone1:(Host *)h
{
if(self.money==h.price)
[h adelegate];
else if(self.money>=h.price)
{
[h answer1:self];
}
else if(self.money<h.price)
[h answer2];
}
啟發:如果代理不遵守協定,oc是支援強行将不遵守協定的對象作為代理的;
我是一個菜雞,如果上面有錯誤或者可優化之處,請各位不吝賜教。