notification,protocol,extension,class的混用

来源:互联网 发布:淘宝不能卖香烟 编辑:程序博客网 时间:2024/06/14 05:41

程序如下:

协议部分:

//定义了一个拍卖的代理协议,里面有四个参数,分别是物主,拍卖物的名字,物主提供的价格,拍卖物年代

#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"


@classCompany;


@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"

@interfaceHost()

-(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=[superinit])

{

    self.name=aName;

    self.owner=aOwner;

    self.price=aPrice;

    self.times=aTimes;

    self.delegate=aDelegate;

    self.jewellery=aJewellery;

//注册通知

   NSNotificationCenter * center=[NSNotificationCenterdefaultCenter];

    [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.delegateowner:self.ownername:self.nameprice:self.pricetimes:self.times];

    

    }

    else

    {

        NSLog(@"没有宝贝提供拍卖");

    

    }



}


//物主收到消息后的反应

-(void)getinfo

{

    

    NSLog(@"%@:我已经知道了",self.owner);

}


//电话回复1

-(void)answer1:(Company *)c

{

   NSLog(@"继续拍卖!");

    [self.delegateowner:self.ownername:self.nameprice:c.moneytimes:self.times];


}

//电话回复2

-(void)answer2

{

   NSLog(@"我不拍卖了!");

}


//释放内存

-(void)dealloc

{  

   NSNotificationCenter * center=[NSNotificationCenterdefaultCenter];

    [centerremoveObserver:selfname:@"auctionResult"object:nil];

    [_namerelease];

    [_ownerrelease];

    [_timesrelease];

    [superdealloc];


}


@end


公司类:

<.h>

#import<Foundation/Foundation.h>

#import"auctionDelegate.h"



@classHost;


@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 =[superinit])

{

    self.money=aMoney;


}


   return self;}


-(void)phone

{

//拍卖成功后,给物主发送通知

   NSNotificationCenter * center=[NSNotificationCenterdefaultCenter];

    [centerpostNotificationName:@"auctionResult"object:self];

    


}

//实现协议方法

-(void)owner:(id)aowner name:(id)aname price:(int)aprice times:(id)atimes

{


    NSLog(@"物主:%@,宝物名称:%@,价钱:%d,年代:%@",aowner,aname,aprice,atimes);

     [selfphone];

    


}


//正常拍卖,价格不可能和物主提供的价格一样,所以拍卖方进行了比较人性化的处理,打电话给物主

//如果价格相等,继续拍卖;如果大于物主价格,调用物主的第一通回复电话,如果小于,调用物主的第二桶电话

-(void)phone1:(Host *)h

{

if(self.money==h.price)

    [h adelegate];

elseif(self.money>=h.price)

{

    [h answer1:self];

}

elseif(self.money<h.price)

    [h answer2];

}




启发:如果代理不遵守协议,oc是支持强行将不遵守协议的对象作为代理的;

我是一个菜鸡,如果上面有错误或者可优化之处,请各位不吝赐教。




原创粉丝点击