zoukankan      html  css  js  c++  java
  • [Objective-C] 006_Protocol(协议)

      学过java的同学都知道Interface(接口),那么在Objective-C中有没有接口呢?其实 Objective-C中用Protocol(协议)来实现的,在Objective-C具体怎么用,我们直接看代码例子。

    StudentProtocol

    ////////////////////    .h    /////////////////////
      
    #import <Foundation/Foundation.h>
    
    @protocol StudentProtocol <NSObject>
    
    @optional   //下面的方法是可选实现的
    - (void)fallInLove:(NSString *)name;
    
    @required  //下面的方法是必须实现的
    - (void)curriculum;
    
    @end                
    

      StudentProtocol 已经写好了那要怎么用呢,我们以Student 类为例,Student要实现StudentProtocol 只需在Student.h 中类名后加入<StudentProtocol>,Student.m 实现StudentProtocol中定义的方法即可。

    ////////////////    .h    ///////////////////
    
    #import "Person.h"
    #import "StudentProtocol.h"
    
    @interface Student : Person <StudentProtocol>
    
    - (id)initWithName:(NSString *)name sex:(NSString *)sex age:(int)age;
    @end
    
    
    ////////////////    .m   ///////////////////
    
    #import "Student.h"
    
    @implementation Student
    
    - (id)initWithName:(NSString *)name sex:(NSString *)sex age:(int)age
    {
        self = [super init];
        if (self) {
            self.name = name;
            self.sex = sex;
            self.age = age;
        }
        return self;
    }
    
    - (Person *)work
    {
        NSLog(@"%@正在工作",self.name);
        return 0;
    }
    
    - (void)printInfo {
        NSLog(@"我的名字叫:%@ 今年%d岁 我是一名%@ %@",self.name,self.age,self.sex,NSStringFromClass([self class]));
    }
    
    #pragma mark StudentProtocol
    
    - (void)fallInLove:(NSString *)name {
        NSLog(@"我还是学生,谈不谈恋爱都可以...但是我还是在和 ---> %@ <--- 谈恋爱",name);
    }
    
    - (void)curriculum {
        NSLog(@"我是学生,必须上课学习...");
    }
    
    @end
    

      在StudentProtocol 声明了两个方法,有一个可选实现,那我们有没有办法知道 Student 到底实现了这个方法呢?有的,继续看代码。

    #import "AppDelegate.h"
    #import "Student.h"
    #import "StudentProtocol.h"
    
    @interface AppDelegate ()
    
    @end
    
    @implementation AppDelegate
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        
        Student *s = [[Student alloc] initWithName:@"小明" sex:@"男" age:12];   
        if ([s conformsToProtocol:@protocol(StudentProtocol)]) {  //判断是否遵循了某个协议
            NSLog(@"我遵循了 --- StudentProtocol --- 协议");
            if ([s respondsToSelector:@selector(fallInLove:)]) {  //判断是否有实现某个方法
                [s fallInLove:@"如花"];
            }
        }
    
        return YES;
    }
    
    @end
    

    测试结果:

    2015-06-14 18:23:13.104 Attendance[16464:617950] 我遵循了 --- StudentProtocol --- 协议

    2015-06-14 18:23:13.104 Attendance[16464:617950] 我还是学生,谈不谈恋爱都可以...,但是我还是在和---> 如花 <---谈恋爱。

     

    本站文章为 宝宝巴士 SD.Team 原创,转载务必在明显处注明:(作者官方网站: 宝宝巴士 
    转载自【宝宝巴士SuperDo团队】 原文链接: http://www.cnblogs.com/superdo/p/4575504.html

     

  • 相关阅读:
    程灵素:我走过山的时候山不说话
    编译原理自学计划
    由一个虚构的例子谈谈中小型研发型项目的技术管理及成本控制(全文)
    用3种IDE导入Linux 2.26 内核源码
    Web风行者的设计方案与计划
    使用pyste自动生成c++类的python wrapper
    让log4cpp日志文件超过2G(Linux下)的方法
    python绑定c++程序
    网络风行者(KSpider)的规则体系结构
    检测您的浏览器是否支持 HTML5 视频方法
  • 原文地址:https://www.cnblogs.com/superdo/p/4575504.html
Copyright © 2011-2022 走看看