Main.m
#import "Children.h"
#import "Nure.h"
#import "Nanny.h"
int main(int argc, const char * argv[])
{
Children *children = [[Children alloc] init];
// Nure *nure = [[Nure alloc] init];
Nanny *nanny = [[Nanny alloc] init];
// [children setNure:nure];
[children setNanny:nanny];
[[NSRunLoop currentRunLoop] run];
return 0;
}
Children.h
@class Nure;
@class Nanny;
@interface Children : NSObject {
NSInteger _index;
Nure *_nure;
Nanny *_nanny;
}
- (void)setNure:(Nure *)nure;
- (void)setNanny:(Nanny *)nanny;
Children.m
#import "Nure.h"
#import "Nanny.h"
@implementation Children
- (id)init {
self = [super init];
if (self ) {
//开启定时器
[NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(timeAction:)userInfo:nil
repeats:YES];
}
return self;
}
- (void)setNure:(Nure *)nure {
_nure = nure;
}
- (void)setNanny:(Nanny *)nanny {
_nanny = nanny;
}
- (void)timeAction:(NSTimer *)time {
_index ++;
if (_index == 5) {
//小孩脏了
// [_nure wash:self];
[_nanny wash:self];
}
if (_index == 10) {
//小孩不高兴了
// [_nure play:self];
[_nanny play:self];
}
}
Nanny.h
#import "Children.h"
@interface Nanny : NSObject
//洗澡
- (void)wash:(Children *)child;
//和小孩玩
- (void)play:(Children *)child;
Nanny.m
@implementation Nanny
//洗澡
- (void)wash:(Children *)child {
NSLog(@"护士给小孩洗澡了");
}
//和小孩玩
- (void)play:(Children *)child {
NSLog(@"护士和小孩玩耍");
}
Nurse.h
#import "Children.h"
@interface Nure : NSObject
//洗澡
- (void)wash:(Children *)child;
//和小孩玩
- (void)play:(Children *)child;
Nurse.m
//洗澡
- (void)wash:(Children *)child {
NSLog(@"保姆给小孩洗澡了");
}
//和小孩玩
- (void)play:(Children *)child {
NSLog(@"保姆和小孩玩耍");
}