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的字符串拼接 往往只 ...
随机推荐
- Vue.js中 watch 的高级用法
假设有如下代码: <div> <p>FullName: {{fullName}}</p> <p>FirstName: <input type=&q ...
- 牛客网 桂林电子科技大学第三届ACM程序设计竞赛 A.串串-后缀自动机模板题
链接:https://ac.nowcoder.com/acm/contest/558/A来源:牛客网 A.串串 小猫在研究字符串. 小猫在研究字串. 给定一个长度为N的字符串S,问所有它的子串Sl…r ...
- h5行情k线开发
前言 由于公司项目需要,要做港股行情的H5版本,经过分析需求,大致有两块难点: 一是行情的推送接收,二是行情K线的生成及相关操作.本文章主要分析行情K线的相关实现,由于我们前端团队之前 ...
- Python 中的面向对象和异常处理
在之前我们已经说过了 Python 中内置的主要的几种对象类型,(数,字符串,列表,元组和字典).而面向对象的核心人物还没出场呢 .那么我们常说的对象是什么类型的呢,其实他的类型就是“类”.继承封装和 ...
- 美团外卖Android平台化的复用实践
美团外卖平台化复用主要是指多端代码复用,正如美团外卖iOS多端复用的推动.支撑与思考文章所述,多端包含有两层意思:其一是相同业务的多入口,指美团外卖业务需要在美团外卖App(下文简称外卖App)和美团 ...
- [代码审计]某开源商城前台getshell
0x00 前言 这套系统搞了有点久了,漏洞是发现了,但一直卡在某个地方迟迟没拿下来. 下面就分享一下自己审这套系统的整个过程. 0x01 系统简介 略 0x02 审计入口 看到inc\functi ...
- 50个必备jQuery代码段
0. 如何创建嵌套的过滤器: 1 2 3 4 5 //允许你减少集合中的匹配元素的过滤器, //只剩下那些与给定的选择器匹配的部分.在这种情况下, //查询删除了任何没(:not)有(:has) // ...
- 1036 Boys vs Girls (25)(25 point(s))
problem This time you are asked to tell the difference between the lowest grade of all the male stud ...
- 【贪心】【后缀自动机】Gym - 101466E - Text Editor
题意:给你两个串A,B,以及一个整数K,让你找到B的一个尽可能长的前缀,使得其在A串中出现的次数不小于K次. 对A串建立后缀自动机,然后把B串放在上面跑,由于每到一个结点,该结点endpos集合的大小 ...
- (转)park1.0.0生态圈一览
转自博客:http://www.tuicool.com/articles/FVBJBjN Spark1.0.0生态圈一览 Spark生态圈,也就是BDAS(伯克利数据分析栈),是伯克利APMLab实验 ...