oc学习之旅:协议 银行转账

协议:TellREcevie.h  与 SendProtocol.h

TellREcevie.h

-(void)tellRecevie:(NSNumber *)money;

SendProtocol.h

-(void)sendMoney:(NSNumber *)money;

流程,userA转钱给银行,银行转钱给B,B收到钱了通知银行,银行通知A


#import <Foundation/Foundation.h>

#import "UserA.h"

#import "UserB.h"

#import "Bank.h"

int main(int argc, const char * argv[])

{


    @autoreleasepool {

        

        Bank * bank = [[[Bank alloc] init] autorelease];

        UserA * a =[[[UserA alloc] init] autorelease];

        UserB * b = [[[UserB alloc] init] autorelease];

        a.delegate = bank;

        [a giveMoney:100];

       bank.delegate = b;

        [bank giveMoney];

        b.delegate = bank;

        [b respondtoBank];

        bank.delegate1 = a;

        [bank tellUserA];

        

    }

    return 0;

}


UserA.h

#import <Foundation/Foundation.h>

#import "SendProtocol.h"

#import "TellRecevie.h"

@interface UserA : NSObject<TellRecevie>

{

    int _count;

    id<SendProtocol>_delegate;

}

@property (nonatomic,assign) int count;

@property (nonatomic,assign) id<SendProtocol>delegate;

-(void)giveMoney:(int) money;

@end


UserA.m

- (id)init

{

    self = [super init];

    if (self) {

        _count=1000;

        NSLog(@"%@:当前账户余额总额为:%d",[self class],_count);

    }

    return self;

}


@synthesize count = _count;

@synthesize delegate = _delegate;


#pragma mark - A to Bank


-(void)giveMoney:(int)money

{

    //c类型必须转换成NSNumber

    if (_delegate && [_delegate respondsToSelector:@selector(sendMoney:)]) {

        NSLog(@"%@:开始转账,金额为%d",[self class],money);

        [_delegate performSelector:@selector(sendMoney:) withObject: [NSNumber numberWithInt:money]];

    }

}

#pragma mark - Bank tell A

-(void)tellRecevie:(NSNumber *)money

{

    _count -= money.intValue;

    NSLog(@"%@:寄款人收到转账,当前余额为:%d",[self class],_count );

}


Bank.h

#import <Foundation/Foundation.h>

#import "SendProtocol.h"

#import "TellRecevie.h"

@interface Bank : NSObject<SendProtocol,TellRecevie>

{

    

    id<SendProtocol> _delegate;

    id<TellRecevie> _delegate1;

}

@property(nonatomic,assign) id<SendProtocol>delegate;

@property(nonatomic,assign) id<TellRecevie>delegate1;

-(void)giveMoney;

-(void)tellUserA;

@end


Bank.m

{

    NSNumber * temp;

}


@synthesize delegate = _delegate;

@synthesize delegate1 = _delegate1;


#pragma mark - A to Bank

-(void)sendMoney:(NSNumber *)money

{

    NSLog(@"%@:receive %@",[self class],money);

    temp = money;

}

#pragma mark - Bank to B

-(void)giveMoney

{

    if (_delegate && [_delegate respondsToSelector:@selector(sendMoney:)]) {

        [_delegate performSelector:@selector(sendMoney:) withObject:temp];

    }

}

#pragma mark - B tell Bank

-(void)tellRecevie:(NSNumber *)money

{

    temp = money;

    NSLog(@"%@:receive 收款人收到金额的信息,收款人收到的金额为:%@",[self class],temp);

}

#pragma mark - Bank tell A

-(void)tellUserA

{

    if (_delegate1 && [_delegate1 respondsToSelector:@selector(tellRecevie:)]) {

        NSLog(@"%@:告诉寄款人收款人已经收到金额%@",[self class],temp);

        [_delegate1 performSelector:@selector(tellRecevie:) withObject:temp];

    }

}


UserB.h

#import <Foundation/Foundation.h>

#import "SendProtocol.h"

#import "TellRecevie.h"

@interface UserB : NSObject<SendProtocol>

{

    int _count;

    id <TellRecevie>_delegate;

    NSNumber *_recevie;

}

@property (nonatomic,assign) id<TellRecevie>delegate;

@property (nonatomic,assign) int count;

@property (nonatomic,assign) NSNumber *recvive;

-(void)respondtoBank;

@end


UserB.m

@implementation UserB


@synthesize delegate = _delegate;

@synthesize count = _count;

@synthesize recvive = _recevie;


- (id)init

{

    self = [super init];

    if (self) {

        _count=1000;

        NSLog(@"%@:当前余额总额为:%d",[self class],_count);

    }

    return self;

}


#pragma mark - A to Bank


-(void)sendMoney:(NSNumber *)money

{

    _count = money.intValue;

    NSLog(@"%@:收到%@元,当前余额账户为%d",[self class],money,_count );

    _recevie = money;

}


#pragma mark = TellRecevie


-(void)respondtoBank

{

    if (_delegate &&[_delegate respondsToSelector:@selector(tellRecevie:)]) {

        NSLog(@"%@:告诉银行,已经收到金额%@",[self class],_recevie);

        [_delegate performSelector:@selector(tellRecevie:) withObject:_recevie];

    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值