notification,protocol,extension,class的混用

notification,protocol,extension,class的混用,第1张

概述程序如下: 协议部分: //定义了一个拍卖的代理协议,里面有四个参数,分别是物主,拍卖物的名字,物主提供的价格,拍卖物年代 #import <Foundation/Foundation.h> @protocol auctionDelegate <NSObject> -(void)owner:(id)aowner         name:(id)aname        price:(int)ap

程序如下:@H_419_7@

协议部分:@H_419_7@

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

@H_419_7@

#import <Foundation/Foundation.h>@H_419_7@


@H_419_7@

@protocol auctionDelegate <NSObject>@H_419_7@

-(voID)owner:(ID)aowner@H_419_7@

        name:(ID)aname@H_419_7@

       price:(int)aprice@H_419_7@

       times:(ID)atimes;@H_419_7@


@H_419_7@

@end@H_419_7@

主人类:@H_419_7@

<.h文件>@H_419_7@

@H_419_7@

#import <Foundation/Foundation.h>@H_419_7@

#import "auctionDelegate.h"@H_419_7@


@H_419_7@

@class Company;@H_419_7@


@H_419_7@

@interface Host : NSObject@H_419_7@


@H_419_7@

{@H_419_7@

    Nsstring * _name;@H_419_7@

    Nsstring *_owner;@H_419_7@

    int _price;@H_419_7@

    Nsstring * _times;@H_419_7@


@H_419_7@

    BOol _jewellery;//是否有宝贝@H_419_7@


@H_419_7@

    ID<auctionDelegate> _delegate;//声明了一个代理@H_419_7@

}@H_419_7@

@property(nonatomic,assign)ID<auctionDelegate>delegate;@H_419_7@

@property(nonatomic,retain)Nsstring * name;@H_419_7@

@property(nonatomic,retain)Nsstring*owner;@H_419_7@

@property(nonatomic,assign)int price;@H_419_7@

@property(nonatomic,retain)Nsstring * times;@H_419_7@

@property(nonatomic,assign)BOol jewellery;@H_419_7@


@H_419_7@

-(ID)initWithname:(Nsstring *)aname@H_419_7@

            Owner:(Nsstring *)aOwner@H_419_7@

            Price:(int)aPrice@H_419_7@

            Times:(Nsstring *)aTimes@H_419_7@

         Delegate:(ID<auctionDelegate>)aDelegate@H_419_7@

         Jewellery:(BOol)aJewellery;@H_419_7@

-(voID)adelegate;//协议传参实例方法@H_419_7@

-(voID)getinfo;//收到通知的反应@H_419_7@


@H_419_7@

-(voID)answer1:(Company *)c;//电话回复1@H_419_7@

-(voID)answer2;//电话回复2@H_419_7@


@H_419_7@

@end@H_419_7@


@H_419_7@

<.m>@H_419_7@

@H_419_7@


@H_419_7@

#import "Host.h"@H_419_7@

//定义了一个延展,里面含有一个私有方法@H_419_7@

#import "Company.h"@H_419_7@

@interface Host()@H_419_7@

-(BOol)treasure;@H_419_7@

@end@H_419_7@


@H_419_7@

@implementation Host@H_419_7@

@synthesize delegate=_delegate;@H_419_7@

@synthesize name=_name;@H_419_7@

@synthesize owner=_owner;@H_419_7@

@synthesize price=_price;@H_419_7@

@synthesize times=_times;@H_419_7@

@synthesize jewellery=_jewellery;@H_419_7@


@H_419_7@

-(ID)initWithname:(Nsstring *)aname@H_419_7@

            Owner:(Nsstring *)aOwner@H_419_7@

            Price:(int)aPrice@H_419_7@

            Times:(Nsstring *)aTimes@H_419_7@

         Delegate:(ID<auctionDelegate>)aDelegate@H_419_7@

        Jewellery:(BOol)aJewellery;@H_419_7@

{@H_419_7@

if(self=[super init])@H_419_7@

{@H_419_7@

    self.name=aname;@H_419_7@

    self.owner=aOwner;@H_419_7@

    self.price=aPrice;@H_419_7@

    self.times=aTimes;@H_419_7@

    self.delegate=aDelegate;@H_419_7@

    self.jewellery=aJewellery;@H_419_7@

//注册通知@H_419_7@

    NSNotificationCenter * center=[NSNotificationCenter defaultCenter];@H_419_7@

    [center addobserver:self selector:@selector(getinfo) name:@"auctionResult" object:nil];@H_419_7@

    @H_419_7@


@H_419_7@

}@H_419_7@


@H_419_7@

    return self;}@H_419_7@


@H_419_7@

//如果有宝贝返回1,没有返回0@H_419_7@

-(BOol)treasure@H_419_7@

{@H_419_7@

    BOol value=0;@H_419_7@

if(self.jewellery==1)@H_419_7@

{@H_419_7@

    value=1;@H_419_7@

}@H_419_7@

    return value;@H_419_7@


@H_419_7@

}@H_419_7@


@H_419_7@

//传参,如果有宝贝,才传参,没有则无法进行拍卖@H_419_7@

-(voID)adelegate@H_419_7@

{@H_419_7@


@H_419_7@

    if([self treasure])@H_419_7@

    {@H_419_7@

        [self.delegate owner:self.owner name:self.name price:self.price times:self.times];@H_419_7@

    @H_419_7@

    }@H_419_7@

    else@H_419_7@

    {@H_419_7@

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

    @H_419_7@

    }@H_419_7@


@H_419_7@


@H_419_7@

}@H_419_7@


@H_419_7@

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

-(voID)getinfo@H_419_7@

{@H_419_7@

    @H_419_7@

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

}@H_419_7@


@H_419_7@

//电话回复1@H_419_7@

-(voID)answer1:(Company *)c@H_419_7@

{@H_419_7@

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

    [self.delegate owner:self.owner name:self.name price:c.money times:self.times];@H_419_7@


@H_419_7@

}@H_419_7@

//电话回复2@H_419_7@

-(voID)answer2@H_419_7@

{@H_419_7@

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

}@H_419_7@


@H_419_7@

//释放内存@H_419_7@

-(voID)dealloc@H_419_7@

{  @H_419_7@

    NSNotificationCenter * center=[NSNotificationCenter defaultCenter];@H_419_7@

    [center removeObserver:self name:@"auctionResult" object:nil];@H_419_7@

    [_name release];@H_419_7@

    [_owner release];@H_419_7@

    [_times release];@H_419_7@

    [super dealloc];@H_419_7@


@H_419_7@

}@H_419_7@


@H_419_7@

@end@H_419_7@


@H_419_7@

公司类:@H_419_7@

<.h>@H_419_7@

@H_419_7@

#import <Foundation/Foundation.h>@H_419_7@

#import "auctionDelegate.h"@H_419_7@


@H_419_7@


@H_419_7@

@class Host;@H_419_7@


@H_419_7@

@interface Company : NSObject<auctionDelegate>@H_419_7@


@H_419_7@

{@H_419_7@

//拍卖会现场提供的报价@H_419_7@

    int _money;@H_419_7@


@H_419_7@

    @H_419_7@

}@H_419_7@

@property(nonatomic,assign)int money;@H_419_7@


@H_419_7@

//在现场供价后,打电话给物主,是否继续进行拍卖@H_419_7@

-(voID)phone1:(Host *)h;@H_419_7@


@H_419_7@

-(ID)initWithMoney:(int)aMoney;@H_419_7@


@H_419_7@

@end@H_419_7@

;@H_419_7@


@H_419_7@

<.m>@H_419_7@

@H_419_7@

#import "Company.h"@H_419_7@

#import "Host.h"@H_419_7@


@H_419_7@


@H_419_7@

@implementation Company@H_419_7@

@synthesize money=_money;@H_419_7@


@H_419_7@

-(ID)initWithMoney:(int)aMoney;@H_419_7@


@H_419_7@

{@H_419_7@

if(self =[super init])@H_419_7@

{@H_419_7@

    self.money=aMoney;@H_419_7@


@H_419_7@

}@H_419_7@


@H_419_7@

    return self;}@H_419_7@


@H_419_7@

-(voID)phone@H_419_7@

{@H_419_7@

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

    NSNotificationCenter * center=[NSNotificationCenter defaultCenter];@H_419_7@

    [center postNotificationname:@"auctionResult" object:self];@H_419_7@

    @H_419_7@


@H_419_7@

}@H_419_7@

//实现协议方法@H_419_7@

-(voID)owner:(ID)aowner name:(ID)aname price:(int)aprice times:(ID)atimes@H_419_7@

{@H_419_7@


@H_419_7@

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

     [self phone];@H_419_7@

    @H_419_7@


@H_419_7@

}@H_419_7@


@H_419_7@

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

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

-(voID)phone1:(Host *)h@H_419_7@

{@H_419_7@

if(self.money==h.price)@H_419_7@

    [h adelegate];@H_419_7@

else if(self.money>=h.price)@H_419_7@

{@H_419_7@

    [h answer1:self];@H_419_7@

}@H_419_7@

else if(self.money<h.price)@H_419_7@

    [h answer2];@H_419_7@

}@H_419_7@


@H_419_7@

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

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

以上是内存溢出为你收集整理的notification,protocol,extension,class的混用全部内容,希望文章能够帮你解决notification,protocol,extension,class的混用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1053736.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存