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 #import9 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 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 @end100 101 102 // main.m103 // 03-动态绑定104 //105 // Created by ma c on 15/8/11.106 // Copyright (c) 2015年. All rights reserved.107 //108 109 #import 110 #import "Fraction.h"111 #import "Complex.h"112 int main(int argc, const char * argv[])113 {114 @autoreleasepool115 {116 //测试分数类117 Fraction *f1 = [[Fraction alloc]initWithNumerator:1118 addDenominator:2];119 [f1 print];120 121 Fraction *f2 = [[Fraction alloc]initWithNumerator:2122 addDenominator:3];123 [f2 print];124 125 Fraction *f3 = [f1 add:f2];126 [f3 print];127 128 //测试复数类129 Complex *c1 = [[Complex alloc]initWithReal:5.0 andImag:3.0];130 [c1 print];131 132 Complex *c2 = [[Complex alloc]initWithReal:4.3 andImag:2.5];133 [c2 print];134 135 Complex *c3 = [c1 add: c2];136 [c3 print];137 138 139 //测试动态绑定140 id pObj = nil;141 pObj = f3;142 [f3 print];143 144 pObj = c3;145 [c3 print];146 147 id arr[3] = {c1,f1,@""};148 for(int i=0;i<3;i++)149 {150 //运行时检查151 /*if([arr[i] isKindOfClass:[Fraction class]]==YES || [arr[i] isKindOfClass:[Complex class]]==YES)152 */153 if([arr[i] respondsToSelector:@selector(print)]==YES)154 {155 [arr[i] print];156 //SEL sel = @selector(print);157 //[arr[i] performSelector:@selector(print)];158 }159 160 161 }162 }163 return 0;164 }