[非凡程序员]代理

#import <Foundation/Foundation.h>
#import "Doctor.h"
#import "Sick.h"
#import "Nurse.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //实例一个病人
        Sick *xiaoming=[[Sick alloc]init];
        xiaoming.name=@"jack";
        
        //实例一个医生
        Doctor *doctor=[[Doctor alloc]init];
        doctor.name=@"医生";
        
        //实例一个护士
        Nurse *nurse=[[Nurse alloc]init];
        nurse.name=@"护士";
        
        //设置病人委托对象,让谁看病,委托对象就是谁
//      xiaoming.sick=nurse;
        xiaoming.sick = doctor;
        
        //找委托对象治病
        xiaoming.ill;
        
    }
    return 0;
}

//协议类
@protocol sickDeleget <NSObject>
//协议的治疗方法
-(void) cure;
@end

//Doctor类h文件 
#import <Foundation/Foundation.h>
#import "cures.h"
@interface Doctor : NSObject<sickDeleget>
@property(nonatomic,strong)NSString *name;
@end

//Doctor类m文件 
#import "Doctor.h"
@implementation Doctor
-(void)cure{
    NSLog(@"医生看病");
}
@end

//Nurse类m文件 
#import <Foundation/Foundation.h>
#import "cures.h"
@interface Nurse : NSObject<sickDeleget>
@property(nonatomic,strong)NSString *name;
@end

//Nurse类m文件 
#import "Nurse.h"
@implementation Nurse
-(void)cure{
    NSLog(@"护士请医生看病");
}
@end


//Sick类m文件 
#import <Foundation/Foundation.h>
#import "cures.h"
@interface Sick : NSObject<sickDeleget>

@property(nonatomic,strong)NSString *name;
//设置代理属性,
@property(nonatomic,strong)id<sickDeleget> sick;
//设置看病的方法
-(void)ill;

@end


//Sick类m文件 
#import "Sick.h"

@implementation Sick
-(void)ill{
    NSLog(@"kanbing");
    [self.sick cure];//调用协议里面的方法
}
@end


你可能感兴趣的:([非凡程序员]代理)