Objective-C:动态绑定
- // Complex.h
- // 03-动态绑定
- //
- // Created by ma c on 15/8/11.
- // Copyright (c) 2015年. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- @interface Complex : NSObject
- @property(nonatomic,assign)CGFloat real;//实部
- @property(nonatomic,assign)CGFloat imag;//虚部
- -(instancetype)initWithReal:(CGFloat)r andImag:(CGFloat)i;
- -(Complex *)add:(Complex *)c;
- -(void)print;
- @end
- // Complex.m
- // 03-动态绑定
- //
- // Created by ma c on 15/8/11.
- // Copyright (c) 2015年 bjsxt. All rights reserved.
- //
- #import "Complex.h"
- @implementation Complex
- -(instancetype)initWithReal:(CGFloat)r andImag:(CGFloat)i
- {
- self = [super init];
- if(self)
- {
- _real = r;
- _imag = i;
- }
- return self;
- }
- -(Complex *)add:(Complex *)c
- {
- CGFloat r = _real+c.real;
- CGFloat i = _imag+c.imag;
- return [[Complex alloc]initWithReal:r andImag:i];
- }
- -(void)print
- {
- NSLog(@"%.2f*%.2fi",_real,_imag);
- }
- @end
- // Fraction.h
- // 03-动态绑定
- //
- // Created by ma c on 15/8/11.
- // Copyright (c) 2015年. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- @interface Fraction : NSObject
- @property(nonatomic,assign)NSInteger numerator;//分子
- @property(nonatomic,assign)NSInteger denominator;//分母
- -(id)initWithNumerator:(NSInteger)n addDenominator:(NSInteger) d;
- -(Fraction*) add:(Fraction*) fraction;
- -(void)print;
- @end
- // Fraction.m
- // 03-动态绑定
- //
- // Created by ma c on 15/8/11.
- // Copyright (c) 2015年 bjsxt. All rights reserved.
- //
- #import "Fraction.h"
- @implementation Fraction
- -(id)initWithNumerator:(NSInteger)n addDenominator:(NSInteger) d
- {
- self = [super init];
- if(self)
- {
- _numerator = n;
- _denominator = d;
- }
- return self;
- }
- -(Fraction*) add:(Fraction*) fraction
- {
- NSInteger n = _numerator*fraction.denominator+fraction.numerator*_denominator;
- NSInteger d = _denominator*fraction.denominator;
- return [[Fraction alloc]initWithNumerator:n addDenominator:d];
- }
- -(void)print
- {
- NSLog(@"%ld/%ld",_numerator,_denominator);
- }
- @end
- // main.m
- // 03-动态绑定
- //
- // Created by ma c on 15/8/11.
- // Copyright (c) 2015年. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- #import "Fraction.h"
- #import "Complex.h"
- int main(int argc, const char * argv[])
- {
- @autoreleasepool
- {
- //测试分数类
- Fraction *f1 = [[Fraction alloc]initWithNumerator:
- addDenominator:];
- [f1 print];
- Fraction *f2 = [[Fraction alloc]initWithNumerator:
- addDenominator:];
- [f2 print];
- Fraction *f3 = [f1 add:f2];
- [f3 print];
- //测试复数类
- Complex *c1 = [[Complex alloc]initWithReal:5.0 andImag:3.0];
- [c1 print];
- Complex *c2 = [[Complex alloc]initWithReal:4.3 andImag:2.5];
- [c2 print];
- Complex *c3 = [c1 add: c2];
- [c3 print];
- //测试动态绑定
- id pObj = nil;
- pObj = f3;
- [f3 print];
- pObj = c3;
- [c3 print];
- id arr[] = {c1,f1,@""};
- for(int i=;i<;i++)
- {
- //运行时检查
- /*if([arr[i] isKindOfClass:[Fraction class]]==YES || [arr[i] isKindOfClass:[Complex class]]==YES)
- */
- if([arr[i] respondsToSelector:@selector(print)]==YES)
- {
- [arr[i] print];
- //SEL sel = @selector(print);
- //[arr[i] performSelector:@selector(print)];
- }
- }
- }
- return ;
- }
Objective-C:动态绑定的更多相关文章
- Automake
Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...
- OC 动态类型,动态绑定,动态加载
OC 动态类型,动态绑定,动态加载 Objective-C具有相当多的动态特性,基本的,也是经常被提到和用到的有 动态类型(Dynamic typing) 动态绑定(Dynamic binding) ...
- 刨根问底Objective-C Runtime
http://chun.tips/blog/2014/11/05/bao-gen-wen-di-objective%5Bnil%5Dc-runtime-(2)%5Bnil%5D-object-and- ...
- Objective-C与C++的区别
1.两者的最大相同:都是从C演化而来的面相对象语言,两者都兼容标准C语言 2.两者的最大不同:Objective-C提供了运行期动态绑定机制,而C++是编译静态绑定,并且通过嵌入类(多重继承)和虚函数 ...
- Objective C Runtime 开发介绍
简介 Objective c 语言尽可能的把决定从编译推迟到链接到运行时.只要可能,它就会动态的处理事情.这就意味着它不仅仅需要一个编译器,也需要一个运行时系统来执行变异好的代码.运行时系统就好像是O ...
- iOS完全自学手册——[三]Objective-C语言速成,利用Objective-C创建自己的对象
1.前言 上一篇已经介绍了App Delegate.View Controller的基本概念,除此之外,分别利用storyboard和纯代码创建了第一个Xcode的工程,并对不同方式搭建项目进行了比较 ...
- 第一章 熟悉Objective -C 编写高质量iOS与OS X代码的52 个有效方法
第一章 熟悉Objective -C 编写高质量iOS与OS X代码的52 个有效方法 第一条: 了解Objective-C 语言的起源 关键区别在于 :使用消息结构的语言,其运行时所应执行 ...
- .Net mvc 根据前台参数动态绑定对象
业务需求:根据前台界面的参数,动态绑定对象 <param name="colNames">属性名拼接字符串</param><param name=&q ...
- Objective C中的ARC的修饰符的使用---- 学习笔记九
#import <Foundation/Foundation.h> @interface Test : NSObject /** * 默认的就是__strong,这里只是做示范,实际使用时 ...
- Objective的字符串拼接 似乎没有Swift方便,但也可以制做一些较为方便的写法
NSString *str1 = @"字符串1"; NSString *str2 = @"字符串2"; //在同样条件下,Objective的字符串拼接 往往只 ...
随机推荐
- linux如何连接移动硬盘
下载第三方插件的地方: http://www.tuxera.com/community/open-source-ntfs-3g/ 这是具体教程: http://hellopyl.blog.51cto. ...
- hdoj1879 继续畅通工程(Prime || Kruskal)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1879 思路 这题和hdoj1102很像,图中的有一些路已经修好了,对于这些已经修好的路,我们令还需要修 ...
- 使用chrales抓包IOS的https(pc+手机)
1.安装SSL证书到手机 点击 Help -> SSL Proxying -> Install Charles Root Certificate on a Mobile Device 2. ...
- PHP接入微信H5支付
开发前配置 进行代码接入前,需在微信后台填写授权回调域名,此域名必须经过ICP备案 开发主要流程 用户下单时选择微信支付 商户进行业务逻辑处理并调用微信统一下单接口,微信H5交易类型为:trade_t ...
- web服务端安全之分布式拒绝服务攻击
一.DDOS攻击的原理分布式拒绝服务,Distributed Denial of Service,利用目标系统网络服务功能缺陷或者直接消耗其系统资源,使得该目标系统无法提供正常的服务.通过大量合法的请 ...
- Django的Form机制小问题
使用Django,我们可以以声明式的方式来定义一个Form,如下: 1 2 3 4 5 # -*- coding: utf-8 -*- from django import forms class S ...
- 常用SQL脚本记录一
20.SUM()和 列+ 统计结果时:如果列里有一行为null,SUM函数会忽略它:如果+,则结果集也为NULL了 19 SUBSTRING (expression,startIndex, endIn ...
- 【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 ...
- 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 ...
- hdu 1732 bfs
题意:推箱子游戏 代码写错居然卡内存!! 搞了两天了 #include <iostream> #include <cstdio> #include <cstring> ...