欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

完整代理的简单实现

程序员文章站 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