您的位置:首页 > 职场人生

[非凡程序员]协议,代理

2015-11-05 00:00 603 查看
摘要: 非凡程序员,感谢 黄杨超ios指导,总结:毕绒超学习笔记

#import <Foundation/Foundation.h>

@interface cureProtocol : NSObject

@end

#import "cureProtocol.h"

@implementation cureProtocol

@end

//治病协议---新协议最好遵守基协议
@protocol sickDeleget <NSObject>
//协议的治疗方法,这里没有指定默认是一定实现的
-(void)cure;

@end

#import <Foundation/Foundation.h>
#import "cureSickProtocol.h"
@interface Doctor : NSObject<sickDeleget>//医生遵守协议

@property ( nonatomic , strong ) NSString * name ;

@end

#import "Doctor.h"

@implementation Doctor
//这个方法是协议中的,协议的方法是谁遵守协议谁实现方法,这里没有接口
-(void)cure{
NSLog(@"医生开始治病~");
}
@end

#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=@"小明";
//实例化医生对象
Doctor *aHua=[[Doctor alloc]init];
aHua.name=@"医生";
//实例化护士对象
Nurse *xiaoBai=[[Nurse alloc]init];
xiaoBai.name=@"护士";

//设置病人委托对象
xiaoMing.doctorDeleget=xiaoBai;
// 生病治病
[xiaoMing ill];

}
return 0;
}

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

#import "Nurse.h"

@implementation Nurse
-(void)cure{
NSLog(@"护士开始治病~");
}
@end

#import <Foundation/Foundation.h>
#import "cureSickProtocol.h"
#import "Doctor.h"
@interface Sick : NSObject
@property( nonatomic , strong ) NSString *name;
//设置代理属性,这个属性为id类型便于其他遵守此协议的对象传入,使代码更灵活
@property( nonatomic , strong )id<sickDeleget> doctorDeleget;
//病人方法,生病去看病
-(void)ill;
@end

#import "Sick.h"

@implementation Sick
-(void)ill{
NSLog(@"%@说,医生我生病了啊。。。",_name);
// 病人通过代理委托医生看病
[self.doctorDeleget cure];
}
@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: