完整代理的简单实现
程序员文章站
2022-06-11 11:47:03
main.m文件 Agent.h Agent.m Person.h Person.m ......
main.m文件
#import <foundation/foundation.h> #import "person.h" int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... person *xw = [person new]; [xw needhouse:@"小区房"]; } return 0; }
agent.h
#import <foundation/foundation.h> @protocol renthousedelegate<nsobject> - (void) renthousedidfinished:(nsstring *)result; @end @interface agent : nsobject @property (nonatomic,assign) id <renthousedelegate>delegate; - (nsstring *)renthouse; @end
agent.m
#import "agent.h" @implementation agent - (nsstring *)renthouse{ if (self.delegate != nil) { nsstring *result = @"房子找到了,为**小区3栋405"; [self.delegate renthousedidfinished:result]; return result; } return nil; } @end
person.h
#import <foundation/foundation.h> #import "agent.h" @interface person : nsobject<renthousedelegate> - (void) needhouse:(nsstring *)require; @end
person.m
#import "person.h" @implementation person - (void)needhouse:(nsstring *)require{ //1.找到中介(需要导入中介类) agent *agent = [agent new]; //2.告诉中介我是谁,(在这里就需要在中介类定义一个属性进行记录) agent.delegate = self; //3.中介去租房子,(在这里需要在中介类里面定义一个租房子的方法) [agent renthouse]; } - (void)renthousedidfinished:(nsstring *)result{ nslog(@"%@",result); } @end
上一篇: 未雨绸缪 迎接大数据时代
下一篇: 解决浏览器自动填充input