1.  
  1. // Complex.h
  2. // 03-动态绑定
  3. //
  4. // Created by ma c on 15/8/11.
  5. // Copyright (c) 2015年. All rights reserved.
  6. //
  7.  
  8. #import <Foundation/Foundation.h>
  9.  
  10. @interface Complex : NSObject
  11. @property(nonatomic,assign)CGFloat real;//实部
  12. @property(nonatomic,assign)CGFloat imag;//虚部
  13. -(instancetype)initWithReal:(CGFloat)r andImag:(CGFloat)i;
  14. -(Complex *)add:(Complex *)c;
  15. -(void)print;
  16. @end
  17.  
  18. // Complex.m
  19. // 03-动态绑定
  20. //
  21. // Created by ma c on 15/8/11.
  22. // Copyright (c) 2015年 bjsxt. All rights reserved.
  23. //
  24.  
  25. #import "Complex.h"
  26.  
  27. @implementation Complex
  28. -(instancetype)initWithReal:(CGFloat)r andImag:(CGFloat)i
  29. {
  30. self = [super init];
  31. if(self)
  32. {
  33. _real = r;
  34. _imag = i;
  35. }
  36. return self;
  37. }
  38. -(Complex *)add:(Complex *)c
  39. {
  40. CGFloat r = _real+c.real;
  41. CGFloat i = _imag+c.imag;
  42. return [[Complex alloc]initWithReal:r andImag:i];
  43. }
  44. -(void)print
  45. {
  46. NSLog(@"%.2f*%.2fi",_real,_imag);
  47. }
  48. @end
  49.  
  50. // Fraction.h
  51. // 03-动态绑定
  52. //
  53. // Created by ma c on 15/8/11.
  54. // Copyright (c) 2015年. All rights reserved.
  55. //
  56.  
  57. #import <Foundation/Foundation.h>
  58.  
  59. @interface Fraction : NSObject
  60.  
  61. @property(nonatomic,assign)NSInteger numerator;//分子
  62. @property(nonatomic,assign)NSInteger denominator;//分母
  63. -(id)initWithNumerator:(NSInteger)n addDenominator:(NSInteger) d;
  64. -(Fraction*) add:(Fraction*) fraction;
  65. -(void)print;
  66. @end
  67.  
  68. // Fraction.m
  69. // 03-动态绑定
  70. //
  71. // Created by ma c on 15/8/11.
  72. // Copyright (c) 2015年 bjsxt. All rights reserved.
  73. //
  74.  
  75. #import "Fraction.h"
  76.  
  77. @implementation Fraction
  78. -(id)initWithNumerator:(NSInteger)n addDenominator:(NSInteger) d
  79. {
  80. self = [super init];
  81. if(self)
  82. {
  83. _numerator = n;
  84. _denominator = d;
  85. }
  86. return self;
  87. }
  88. -(Fraction*) add:(Fraction*) fraction
  89. {
  90. NSInteger n = _numerator*fraction.denominator+fraction.numerator*_denominator;
  91. NSInteger d = _denominator*fraction.denominator;
  92.  
  93. return [[Fraction alloc]initWithNumerator:n addDenominator:d];
  94. }
  95. -(void)print
  96. {
  97. NSLog(@"%ld/%ld",_numerator,_denominator);
  98. }
  99. @end
  100.  
  101. // main.m
  102. // 03-动态绑定
  103. //
  104. // Created by ma c on 15/8/11.
  105. // Copyright (c) 2015年. All rights reserved.
  106. //
  107.  
  108. #import <Foundation/Foundation.h>
  109. #import "Fraction.h"
  110. #import "Complex.h"
  111. int main(int argc, const char * argv[])
  112. {
  113. @autoreleasepool
  114. {
  115. //测试分数类
  116. Fraction *f1 = [[Fraction alloc]initWithNumerator:
  117. addDenominator:];
  118. [f1 print];
  119.  
  120. Fraction *f2 = [[Fraction alloc]initWithNumerator:
  121. addDenominator:];
  122. [f2 print];
  123.  
  124. Fraction *f3 = [f1 add:f2];
  125. [f3 print];
  126.  
  127. //测试复数类
  128. Complex *c1 = [[Complex alloc]initWithReal:5.0 andImag:3.0];
  129. [c1 print];
  130.  
  131. Complex *c2 = [[Complex alloc]initWithReal:4.3 andImag:2.5];
  132. [c2 print];
  133.  
  134. Complex *c3 = [c1 add: c2];
  135. [c3 print];
  136.  
  137. //测试动态绑定
  138. id pObj = nil;
  139. pObj = f3;
  140. [f3 print];
  141.  
  142. pObj = c3;
  143. [c3 print];
  144.  
  145. id arr[] = {c1,f1,@""};
  146. for(int i=;i<;i++)
  147. {
  148. //运行时检查
  149. /*if([arr[i] isKindOfClass:[Fraction class]]==YES || [arr[i] isKindOfClass:[Complex class]]==YES)
  150. */
  151. if([arr[i] respondsToSelector:@selector(print)]==YES)
  152. {
  153. [arr[i] print];
  154. //SEL sel = @selector(print);
  155. //[arr[i] performSelector:@selector(print)];
  156. }
  157.  
  158. }
  159. }
  160. return ;
  161. }
  1.  
  1.  

Objective-C:动态绑定的更多相关文章

  1. Automake

    Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...

  2. OC 动态类型,动态绑定,动态加载

    OC 动态类型,动态绑定,动态加载 Objective-C具有相当多的动态特性,基本的,也是经常被提到和用到的有 动态类型(Dynamic typing) 动态绑定(Dynamic binding) ...

  3. 刨根问底Objective-C Runtime

    http://chun.tips/blog/2014/11/05/bao-gen-wen-di-objective%5Bnil%5Dc-runtime-(2)%5Bnil%5D-object-and- ...

  4. Objective-C与C++的区别

    1.两者的最大相同:都是从C演化而来的面相对象语言,两者都兼容标准C语言 2.两者的最大不同:Objective-C提供了运行期动态绑定机制,而C++是编译静态绑定,并且通过嵌入类(多重继承)和虚函数 ...

  5. Objective C Runtime 开发介绍

    简介 Objective c 语言尽可能的把决定从编译推迟到链接到运行时.只要可能,它就会动态的处理事情.这就意味着它不仅仅需要一个编译器,也需要一个运行时系统来执行变异好的代码.运行时系统就好像是O ...

  6. iOS完全自学手册——[三]Objective-C语言速成,利用Objective-C创建自己的对象

    1.前言 上一篇已经介绍了App Delegate.View Controller的基本概念,除此之外,分别利用storyboard和纯代码创建了第一个Xcode的工程,并对不同方式搭建项目进行了比较 ...

  7. 第一章 熟悉Objective -C 编写高质量iOS与OS X代码的52 个有效方法

    第一章 熟悉Objective -C   编写高质量iOS与OS  X代码的52 个有效方法   第一条: 了解Objective-C 语言的起源 关键区别在于 :使用消息结构的语言,其运行时所应执行 ...

  8. .Net mvc 根据前台参数动态绑定对象

    业务需求:根据前台界面的参数,动态绑定对象 <param name="colNames">属性名拼接字符串</param><param name=&q ...

  9. Objective C中的ARC的修饰符的使用---- 学习笔记九

    #import <Foundation/Foundation.h> @interface Test : NSObject /** * 默认的就是__strong,这里只是做示范,实际使用时 ...

  10. Objective的字符串拼接 似乎没有Swift方便,但也可以制做一些较为方便的写法

    NSString *str1 = @"字符串1"; NSString *str2 = @"字符串2"; //在同样条件下,Objective的字符串拼接 往往只 ...

随机推荐

  1. linux如何连接移动硬盘

    下载第三方插件的地方: http://www.tuxera.com/community/open-source-ntfs-3g/ 这是具体教程: http://hellopyl.blog.51cto. ...

  2. hdoj1879 继续畅通工程(Prime || Kruskal)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1879 思路 这题和hdoj1102很像,图中的有一些路已经修好了,对于这些已经修好的路,我们令还需要修 ...

  3. 使用chrales抓包IOS的https(pc+手机)

    1.安装SSL证书到手机 点击 Help -> SSL Proxying -> Install Charles Root Certificate on a Mobile Device 2. ...

  4. PHP接入微信H5支付

    开发前配置 进行代码接入前,需在微信后台填写授权回调域名,此域名必须经过ICP备案 开发主要流程 用户下单时选择微信支付 商户进行业务逻辑处理并调用微信统一下单接口,微信H5交易类型为:trade_t ...

  5. web服务端安全之分布式拒绝服务攻击

    一.DDOS攻击的原理分布式拒绝服务,Distributed Denial of Service,利用目标系统网络服务功能缺陷或者直接消耗其系统资源,使得该目标系统无法提供正常的服务.通过大量合法的请 ...

  6. Django的Form机制小问题

    使用Django,我们可以以声明式的方式来定义一个Form,如下: 1 2 3 4 5 # -*- coding: utf-8 -*- from django import forms class S ...

  7. 常用SQL脚本记录一

    20.SUM()和 列+ 统计结果时:如果列里有一行为null,SUM函数会忽略它:如果+,则结果集也为NULL了 19 SUBSTRING (expression,startIndex, endIn ...

  8. 【CF 453A】 A. Little Pony and Expected Maximum(期望、快速幂)

    A. Little Pony and Expected Maximum time limit per test 1 second memory limit per test 256 megabytes ...

  9. UOJ275 组合数问题

    给定n,m和k,求有多少对(i , j)满足0 ≤ i ≤ n, 0 ≤ j ≤ min(i ,m)且C(︀i,j)︀是k的倍数.n,m ≤ 1018, k ≤ 100,且k是质数. 把i和j都看成k ...

  10. hdu 1732 bfs

    题意:推箱子游戏 代码写错居然卡内存!! 搞了两天了 #include <iostream> #include <cstdio> #include <cstring> ...