zoukankan      html  css  js  c++  java
  • OC8_代理基本概念

    //
    //  ProtectedDelegate.h
    //  OC8_代理基本概念
    //
    //  Created by zhangxueming on 15/6/24.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @protocol ProtectedDelegate <NSObject>
    
    - (void)bark;
    
    @end
    
    
    
    //
    //  Person.h
    //  OC8_代理基本概念
    //
    //  Created by zhangxueming on 15/6/24.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    #import "ProtectedDelegate.h"
    
    @interface Person : NSObject
    {
        id <ProtectedDelegate>_delegate;
    }//对象指针也要遵守协议
    
    @property (retain, nonatomic)id <ProtectedDelegate>delegate;
    
    - (void)go;
    
    @end
    
    
    //
    //  Person.m
    //  OC8_代理基本概念
    //
    //  Created by zhangxueming on 15/6/24.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #import "Person.h"
    
    @implementation Person
    
    - (void)go
    {
        [_delegate bark];
    }
    
    @end
    
    
    //
    //  Cat.h
    //  OC8_代理基本概念
    //
    //  Created by zhangxueming on 15/6/24.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    #import "ProtectedDelegate.h"//包含头文件
    
    @interface Cat : NSObject <ProtectedDelegate>//遵守这个协议
    
    @end
    
    
    //
    //  Cat.m
    //  OC8_代理基本概念
    //
    //  Created by zhangxueming on 15/6/24.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #import "Cat.h"
    
    @implementation Cat
    - (void)bark
    {
        NSLog(@"Miao miao miao ...");
    }
    @end
    
    
    
    
    //
    //  Dog.h
    //  OC8_代理基本概念
    //
    //  Created by zhangxueming on 15/6/24.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    #import "ProtectedDelegate.h"
    
    @interface Dog : NSObject <ProtectedDelegate>
    
    
    @end
    
    
    
    //
    //  Dog.m
    //  OC8_代理基本概念
    //
    //  Created by zhangxueming on 15/6/24.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #import "Dog.h"
    
    @implementation Dog
    
    - (void)bark
    {
        NSLog(@"Wang wang wang ...");
    }
    
    @end
    
    
    
    //
    //  main.m
    //  OC8_代理基本概念
    //
    //  Created by zhangxueming on 15/6/24.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    #import "Dog.h"
    #import "Cat.h"
    #import "Person.h"
    
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            Person *xiaoXin = [[Person alloc] init];
            Dog *dog = [[Dog alloc] init];
            xiaoXin.delegate = dog;
            [xiaoXin go];
            Cat *cat = [[Cat alloc] init];
            xiaoXin.delegate = cat;
            [xiaoXin go];
        }
        return 0;
    }
  • 相关阅读:
    远景GIS云产品规划
    远景GIS云上线
    远景WEBGIS平台实现客户端SHP文件加载
    GIS平台结构设计
    一款基于HTML5的高性能WEBGIS介绍
    Git分布式工作流程
    Git服务器搭建
    Linux入门-9 软件管理基础(CentOS)
    Linux入门-8 Linux系统启动详解
    Linux入门-7 Linux管道、重定向以及文本处理
  • 原文地址:https://www.cnblogs.com/0515offer/p/4598564.html
Copyright © 2011-2022 走看看