OBJECTIVE-C: Dynamic Binding

Source: Internet
Author: User

1 //Complex.h2 //03-Dynamic binding3 //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 Ten @interfaceComplex:nsobject One@property (nonatomic,assign) cgfloat real;//Real Department A@property (nonatomic,assign) cgfloat imag;//Imaginary Part --(Instancetype) Initwithreal: (cgfloat) R andimag: (cgfloat) i; --(Complex *) Add: (Complex *) C; the-(void) print; - @end -  - //COMPLEX.M + //03-Dynamic binding - // + //Created by Ma C on 15/8/11. A //Copyright (c) 2015 BJSXT. All rights reserved. at // -  - #import "Complex.h" -  - @implementationComplex --(Instancetype) Initwithreal: (cgfloat) R andimag: (cgfloat) I in { -Self =[Super init]; to     if(self) +     { -_real =R; the_imag =i; *     } $     returnSelf ;Panax Notoginseng } --(Complex *) Add: (Complex *) C the { +CGFloat r = _real+c.real; ACGFloat i = _imag+C.imag; the     return[[Complex alloc]initwithreal:r andimag:i]; + } --(void) Print $ { $NSLog (@"%.2f*%.2fi", _real,_imag); - } - @end the  - //Fraction.hWuyi //03-Dynamic binding the // - //Created by Ma C on 15/8/11. Wu //Copyright (c) 2015 BJSXT. All rights reserved. - // About  $ #import<Foundation/Foundation.h> -  - @interfaceFraction:nsobject -  A@property (nonatomic,assign) Nsinteger numerator;//molecule +@property (nonatomic,assign) Nsinteger denominator;//Denominator the-(ID) Initwithnumerator: (Nsinteger) n adddenominator: (Nsinteger) D; --(fraction*) Add: (fraction*) fraction; $-(void) print; the @end the  the //fraction.m the //03-Dynamic binding - // in //Created by Ma C on 15/8/11. the //Copyright (c) 2015 BJSXT. All rights reserved. the // About  the #import "Fraction.h" the  the @implementationFraction +-(ID) Initwithnumerator: (Nsinteger) n adddenominator: (nsinteger) d - { theSelf =[Super init];Bayi     if(self) the     { the_numerator =N; -_denominator =D; -     } the     returnSelf ; the } the-(fraction*) Add: (fraction*) Fraction the { -Nsinteger n = _numerator*fraction.denominator+fraction.numerator*_denominator; theNsinteger d = _denominator*Fraction.denominator; the      the    return[[Fraction alloc]initwithnumerator:n adddenominator:d];94 } the-(void) Print the { theNSLog (@"%ld/%ld", _numerator,_denominator);98 } About @end - 101 102 //main.m103 //03-Dynamic binding104 // the //Created by Ma C on 15/8/11.106 //Copyright (c) 2015 BJSXT. All rights reserved.107 //108 109 #import<Foundation/Foundation.h> the #import "Fraction.h"111 #import "Complex.h" the intMainintargcConst Char*argv[])113 { the @autoreleasepool the     { the         //Test Score Class117Fraction *F1 = [[Fraction alloc]initwithnumerator:1118Adddenominator:2];119 [F1 print]; -         121Fraction *F2 = [[Fraction alloc]initwithnumerator:2122Adddenominator:3];123 [F2 print];124          theFraction *F3 =[F1 ADD:F2];126 [F3 print];127          -         //test the plural class129Complex *C1 = [[Complex alloc]initwithreal:5.0Andimag:3.0]; the [C1 print];131          theComplex *C2 = [[Complex alloc]initwithreal:4.3Andimag:2.5];133 [C2 print];134         135Complex *C3 =[C1 add:c2];136 [C3 print];137         138         139         //testing Dynamic Bindings $         IDPOBJ =Nil;141POBJ =f3;142 [F3 print];143         144POBJ =C3;145 [C3 print];146         147         IDarr[3] = {C1,F1,@""};148          for(intI=0;i<3; i++)149         { Max             //Run-time check151             /*if ([arr[i] iskindofclass:[fraction Class]]==yes | | [Arr[i] Iskindofclass:[complex Class]]==yes) the             */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              the             161         }162     }163     return 0;164}

OBJECTIVE-C: Dynamic Binding

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.