协议的声明:

 //
// Person.h
// TestOC01
//
// Created by xinye on 13-10-23.
// Copyright (c) 2013年 xinye. All rights reserved.
// #import <Foundation/Foundation.h> @protocol Person <NSObject> @property (nonatomic,strong) NSString *firstName;
@property (nonatomic,strong) NSString *lastName;
@property (nonatomic,unsafe_unretained) NSUInteger age; @optional
-(id<Person>) initWithFirstName:(NSString *) firstName
lastName:(NSString *) lastName
age:(NSUInteger) age;
@required
-(id<Person>) initWithNil;
@end

实现协议:

 //
// Father.h
// TestOC01
//
// Created by xinye on 13-10-23.
// Copyright (c) 2013年 xinye. All rights reserved.
// #import <Foundation/Foundation.h>
#import "Person.h" @interface Father : NSObject <Person> +(void) sayNil; @end //
// Father.m
// TestOC01
//
// Created by xinye on 13-10-23.
// Copyright (c) 2013年 xinye. All rights reserved.
// #import "Father.h" @implementation Father
// 实现一个协议,必须实现其@required标记的方法,并且必须@synthesize协议中定义的@requeired属性,协议中定义的方法和属性默认都是@required的
@synthesize firstName,lastName,age; -(id<Person>) initWithFirstName:(NSString *)_firstName lastName:(NSString *)_lastName age:(NSUInteger)_age
{
self = [super init];
if (self) {
self.firstName = _firstName;
self.lastName = _lastName;
self.age = _age;
} return self;
} -(id<Person>) initWithNil
{
self = [super init];
return self;
} +(void) sayNil
{
NSLog(@"say Nil Method");
}
@end

测试:

 //
// main.m
// TestOC01
//
// Created by xinye on 13-10-23.
// Copyright (c) 2013年 xinye. All rights reserved.
// #import <Foundation/Foundation.h>
#import "Person.h"
#import "Father.h" int main(int argc, const char * argv[])
{ @autoreleasepool { id<Person> per = [[Father alloc]initWithFirstName:@"张" lastName:@"三" age:]; NSLog(@"姓名:%@",[[per firstName] stringByAppendingString:per.lastName]);
NSLog(@"年龄:%li",per.age);
// 检测是否有实例方法
if([Father instancesRespondToSelector:@selector(initWithNil)]){
NSLog(@"*****Father 类中有一个实例方法:initWithNil");
}else{
NSLog(@"Father 类中没有initWithNil实例方法");
} // 检测是否有类方法
if([Father respondsToSelector:@selector(sayNil)]){
NSLog(@"*****Father 类中有sayNil类方法");
}else{
NSLog(@"Father 类中没有sayNil类方法");
} // 检测是否有实例方法
if([per respondsToSelector:@selector(initWithFirstName:lastName:age:)]){
NSLog(@"*****Father 类中有initWithFirstName:lastName:age:实例方法");
}else{
NSLog(@"Father 类中没有initWithFirstName:lastName:age:实例方法");
} // 检测指定的类是否存在
if(NSClassFromString(@"NSString") != nil){
NSLog(@"=========当前版本中存在NSString类");
}else{
NSLog(@"$$$$$$$$$当前版本中不存在NSString类");
}
if(NSClassFromString(@"NBString") != nil){
NSLog(@"=========当前版本中存在NBString类");
}else{
NSLog(@"$$$$$$$$$当前版本中不存在NBString类");
} }
return ;
}

Objective-C 协议和运行时检查方法、类是否存在的更多相关文章

  1. [源码解析] PyTorch 流水线并行实现 (3)--切分数据和运行时系统

    [源码解析] PyTorch 流水线并行实现 (3)--切分数据和运行时系统 目录 [源码解析] PyTorch 流水线并行实现 (3)--切分数据和运行时系统 0x00 摘要 0x01 分割小批次 ...

  2. 深入理解OOP(三):多态和继承(动态绑定和运行时多态)

    在前面的文章中,我们介绍了编译期多态.params关键字.实例化.base关键字等.本节我们来关注另外一种多态:运行时多态, 运行时多态也叫迟绑定. 深入理解OOP(一):多态和继承(初期绑定和编译时 ...

  3. [转] Java 的泛型擦除和运行时泛型信息获取

    原文链接 https://my.oschina.net/lifany/blog/875769 前言 现在很多程序员都会在简历中写上精通 Java.但究竟怎样才算是精通 Java 呢?我觉得不仅要熟练掌 ...

  4. Java虚拟机系列一:一文搞懂 JVM 架构和运行时数据区

    前言 之前写博客一直比较随性,主题也很随意,就是想到什么写什么,对什么感兴趣就写什么.虽然写起来无拘无束,自在随意,但也带来了一些问题,每次写完一篇后就要去纠结下一篇到底写什么,看来选择太多也不是好事 ...

  5. 排错-windows下 ORA-12560 TNS 协议适配器错误解决方法

    排错-windows下_ORA-12560 TNS 协议适配器错误解决方法 by:授客 QQ:1033553122 问题描述: 修改SQL*Plus窗口属性后,重新打开SQL*Plus时出现ORA-1 ...

  6. DataTable和DataRow利用反射直接转换为Model对象的扩展方法类

    DataTable和DataRow利用反射直接转换为Model对象的扩展方法类   /// <summary> /// 类 说 明:给DataTable和DataRow扩展方法,直接转换为 ...

  7. Objective-C面向对象-对象和类

    文章都是先由本人个人博客:孙占兴:www.teilim.com,先更新,随后CSDN博客才会更新,掌握第一动态请关注本人主站. 原文链接:http://www.teilim.com/objective ...

  8. Java编译时常量和运行时常量

    Java编译时常量和运行时常量 编译期常量指的就是程序在编译时就能确定这个常量的具体值. 非编译期常量就是程序在运行时才能确定常量的值,因此也称为运行时常量. 在Java中,编译期常量指的是用fina ...

  9. day20-Python运维开发基础(装饰器 / 类中的方法 / 类的方法变属性)

    1. 装饰器 / 类中的方法 / 类的方法变属性 # ### 装饰器 """ 定义:装饰器用于拓展原来函数功能的一种语法,返回新函数替换旧函数 优点:在不更改原函数代码的 ...

随机推荐

  1. Hive Tuning(五) 标准调优清单

    Hive的标准调优清单,我们可以对照着来做我们的查询优化!

  2. 根据城市表生成json数据

    T_CityManager am = new T_CityManager(); string tou = "var LAreaData=["; string value = &qu ...

  3. Redis键

    Redis的keys命令用于管理键.使用Redis的keys命令语法如下所示: 语法 redis 127.0.0.1:6379> COMMAND KEY_NAME 例子 redis 127.0. ...

  4. 加密安装Kali Linux条件

    加密安装Kali Linux条件安装Kali Linux到你的电脑过程很简单.首先你需要兼容的电脑硬件.最低硬件要求如下,更好的硬件性能会更好.i386镜象默认使用PAE内核,所以你能在大于4GB内存 ...

  5. Java容器有哪些?

    网易面试: 问:Java容器有哪些,你聊一聊吧 Java容器: 数组,String,java.util下的集合容器 数组长度限制为 Integer.Integer.MAX_VALUE; String的 ...

  6. Java注释规范整理

    Version:0.9 StartHTML:-1 EndHTML:-1 StartFragment:00000099 EndFragment:00018736 在软件开发的过程中总是强调注释的规范,但 ...

  7. 生成基于Maven的项目文档站点

    在Maven中,可以使用“mvn site”,为您的项目信息生成文档站点. mvn site 生成的网站是在项目的“target/site”文件夹中. mvn site 示例 请参见通过“mvn si ...

  8. 【ZooKeeper Notes】系列文章

    [ZooKeeper Notes]系列文章 https://my.oschina.net/xiaotian120/blog/194401

  9. Android Notification和权限机制探讨

    近期为了在部门内做一次小型的技术分享.深入了解了一下Notification的实现原理.以及android的权限机制.在此做个记录.文章可能比較长,没耐心的话就直接看题纲吧. 先看一下以下两张图 图一 ...

  10. 有空研究一下 superwebsocket (底层是 supersocket) 用来实现 web聊天什么的

    参考:http://superwebsocket.codeplex.com/ 一个老外写的 asp.net 下socket的多钟方案推荐 :http://www.codeproject.com/Art ...