• 协议1


    //结构:

    //main.m

     1 #import <Foundation/Foundation.h>
     2 #import "Rent1.h"
     3 #import "Rent2.h"
     4 #import "Rent3.h"
     5 #import "Person.h"
     6 
     7 int main(int argc, const char * argv[]) {
     8     @autoreleasepool {
     9      
    10         Person *xiaowang=[[Person alloc]init];
    11         
    12         Rent1 *r1=[[Rent1 alloc]init];
    13         r1.name=@"Li";
    14         Rent2 *r2=[[Rent2 alloc]init];
    15         r2.name=@"叶良辰";
    16         Rent3 *r3=[[Rent3 alloc]init];
    17         r3.name=@"赵日天";
    18         
    19         xiaowang.obj=r1;
    20         
    21         [xiaowang needHouse];
    22      
    23     }
    24     return 0;
    25 }

    //RentPototol.h《协议》

    #import <Foundation/Foundation.h>
    
    @protocol RentPototol <NSObject>
    @optional
    -(void)rentHouse;
    
    @end

    //Person.h

     1 #import <Foundation/Foundation.h>
     2 #import "RentPototol.h"
     3 @interface Person : NSObject
     4 
     5 @property(nonatomic,strong)NSString *myName;
     6 @property(nonatomic,assign)id<RentPototol> obj;
     7 
     8 -(void)needHouse;
     9 
    10 @end

    //Person.m

     1 #import "Person.h"
     2 #import "RentPototol.h"
     3 
     4 @implementation Person
     5 
     6 -(void)needHouse
     7 {
     8     if ([_obj respondsToSelector:@selector(rentHouse)]) {
     9         [_obj rentHouse];
    10     }
    11     else
    12     {
    13         NSLog(@"臣妾做不到呀!");
    14     }
    15 }
    16 @end

    //Rent1.h

    #import <Foundation/Foundation.h>
    #import "RentPototol.h"
    
    @interface Rent1 : NSObject<RentPototol>
    @property(nonatomic,copy)NSString *name;
    
    @end

    //Rent1.m

    #import "Rent1.h"
    
    @implementation Rent1
    
    -(void)rentHouse
    {
        NSLog(@"我是%@,我打电话找房源",_name);
    }
    
    @end

    //Rent2.h

    #import <Foundation/Foundation.h>
    #import "RentPototol.h"
    
    @interface Rent2 : NSObject<RentPototol>
    @property(nonatomic,copy)NSString *name;
    
    @end

    //Rent2.m

    #import "Rent2.h"
    
    @implementation Rent2
    
    -(void)rentHouse
    {
        NSLog(@"我是%@,我打电话找房源",_name);
    }
    
    @end

    //Rent3.h

    #import <Foundation/Foundation.h>
    #import "RentPototol.h"
    
    @interface Rent3 : NSObject<RentPototol>
    
    @property(nonatomic,copy)NSString *name;
    
    @end

    //Rent3.m

    #import "Rent3.h"
    
    @implementation Rent3
    
    //并没有提供租住的能力,无法实现声明的协议要求
    
    @end

    //结果

    2015-11-10 19:18:15.737 RentHouse[7738:124003] 我是Li,我打电话找房源
  • 相关阅读:
    数据库周刊33丨腾讯Tbase新版本发布;“2020数据技术嘉年华”有奖话题遴选;阿里云技术面试题;APEX 实现数据库自动巡检;MYSQL OCP题库……
    常用ASCII码对照表
    linux 环境root用户新建用户和删除用户
    Utl_Raw.Cast_To_Raw(dbms_obfuscation_toolkit.md5())
    months_between()
    GREATEST(),ROUND(),
    TRUNC()
    oracle+function
    oracle存储过程----遍历游标的方法(for、fetch、while)
    oracle+seqTest
  • 原文地址:https://www.cnblogs.com/liuyingjie/p/4954037.html
Copyright © 2020-2023  润新知