委托方要遵守代理,就要继承代理,并且要实现代理
/*1. 老板不定期的组织活动,每次都让小明统计参加活动的人数,小明每次将参加活动的人数上报给老板,
老板很高兴。*/
//main
/*1. 老板不定期的组织活动,每次都让小明统计参加活动的人数,小明每次将参加活动的人数上报给老板,
老板很高兴。*/
#import <Foundation/Foundation.h>
#import "Boss.h"
#import "XiaoWang.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
XiaoWang *wang = [XiaoWang new];
Boss *boss = [Boss new];
boss.delegate = wang;
wang.agent = boss;
[boss asknum];
}
return 0;
}
//CountNum.h
// CountNum.h
// homework
//
// Created by hehe on 15/9/7.
// Copyright (c) 2015年 wang.hehe. All rights reserved.
//
#import <Foundation/Foundation.h>
@protocol CountNum <NSObject>
- (void)cntnum:(int)i;
@end
//Boss.h
//
// Boss.h
// homework
//
// Created by hehe on 15/9/7.
// Copyright (c) 2015年 wang.hehe. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "CountNum.h"
#import "XiaoWang.h"
@interface Boss : NSObject<CountNum>
@property (nonatomic,strong) XiaoWang * delegate;
- (void)asknum;
@end
//Boss.m
//
// Boss.m
// homework
//
// Created by hehe on 15/9/7.
// Copyright (c) 2015年 wang.hehe. All rights reserved.
//
#import "Boss.h"
@implementation Boss
- (void)cntnum:(int)i
{
NSLog(@"人数是:%d老板很高兴...",i);
}
- (void)asknum
{
NSLog(@"老板问人帮他干活...");
[self.delegate dowork];
}
@end
//XiaoWang.h
//
// XiaoWang.h
// homework
//
// Created by hehe on 15/9/7.
// Copyright (c) 2015年 wang.hehe. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "CountNum.h"
@interface XiaoWang : NSObject
@property (weak) id <CountNum> agent;
- (void)dowork;
@end
//XiaoWang.m
//
// XiaoWang.m
// homework
//
// Created by hehe on 15/9/7.
// Copyright (c) 2015年 wang.hehe. All rights reserved.
//
#import "XiaoWang.h"
@implementation XiaoWang
- (void)dowork
{
NSLog(@"小王开始帮老板干活..");
NSLog(@"小王干完活了");
[self.agent cntnum:20];
}
@end