Boss类的声明文件
#import <Foundation/Foundation.h>
@protocol BossDelegate
-(void)bookTicket;//预定机票
-(void)bookRoom;//预定房间
-(void)notify;//开会
@end
@interface Boss : NSObject
@property(nonatomic,weak)id<BossDelegate>delegate;
-(void)travel;//旅行
-(void)metting;//开会
@end
Boss类的实现文件:
#import "Boss.h"
@implementation Boss
-(void)travel
{
//1.订机票
[_delegate bookTicket];
//2.订房间
[_delegate bookRoom];
//3.去旅行
NSLog(@"boss travel.");
}
-(void)metting
{
//1.通知开会
[_delegate notify];
//2.老板讲话
NSLog(@"boss speak:hello!everybody.");
}
@end
员工的声明文件:
#import <Foundation/Foundation.h>
#import "Boss.h"
@interface Person : NSObject<BossDelegate>
@end
员工的实现文件:
#import "Person.h"
@implementation Person
-(void)bookTicket{
NSLog(@"person bookTicket.");
}
-(void)bookRoom
{
NSLog(@"person bookRoom.");
}
-(void)notify
{
NSLog(@"person metting.");
}
@end
//测试文件
#import <Foundation/Foundation.h>
#import "Boss.h"
#import "Person.h"
#import "Student.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Boss *boss = [Boss new];
Person *person = [Person new];
[boss setDelegate:person];
[boss travel];
[boss metting];
//现在老板计划招一名学生当助理,有助理来完成以上工作
Student *stu = [Student new];
[boss setDelegate:stu];
[boss travel];
[boss metting];
}
return 0;
}