Objective-C:動態綁定

來源:互聯網
上載者:User

標籤:


  1 //  Complex.h  2 //  03-動態綁定  3 //  4 //  Created by ma c on 15/8/11.  5 //  Copyright (c) 2015年 bjsxt. 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年 bjsxt. 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 @end100 101 102 //  main.m103 //  03-動態綁定104 //105 //  Created by ma c on 15/8/11.106 //  Copyright (c) 2015年 bjsxt. All rights reserved.107 //108 109 #import <Foundation/Foundation.h>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 }

 

 

 

Objective-C:動態綁定

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.