关于OC 语言中的协议

来源:互联网 发布:约瑟夫环数据 编辑:程序博客网 时间:2024/05/22 01:39

     举个列子  :一个人要在某地租房,但是自己要上班,所以就要委托中介帮忙。但是同时这个人就要和中介实现一个关于找房子的协议。


#import "Person.h"

#import "LookingForApartment.h"

@implementation Person

-(void)setAgent:(Agent *)agent;

{

    _agent = agent;//中介

}

-(void)setName:(NSString*)name;

{


    _name = name;//找房子的人

}

-(void)setDelegate:(id<LookingForApartment>)delegate;{


  

    _delegate = delegate;//能帮忙找房子人的类

}

-(void)findHouse;{

    

    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(doTime:) userInfo:nil repeats:YES];

 //定时器

  



}

-(void)doTime:(NSTimer *)timer{

    long result = [_delegate LookingForApartment:self];

    if (result<2000) {

        NSLog(@"%ld价格合理 ,租房子了",result);//判断房价

        [timer  invalidate];//找到房子 ,定时器就结束了

    }

    


    }



@end


这个是在找房子的人类做的一些实现。

#import <Foundation/Foundation.h>

@class Person;

@protocol LookingForApartment <NSObject>

-(long)LookingForApartment:(Person *)person;


@end

这个是协议文件

0 0
原创粉丝点击