码迷,mamicode.com
首页 > 其他好文 > 详细

多态性

时间:2016-01-06 14:07:18      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

main.m

// Fraction  分数

// Complex   复数

 

#import <Foundation/Foundation.h>

#import "Fraction.h"

#import "Complex.h"

int main(int argc, constchar * argv[]) {

    @autoreleasepool {

       

        Fraction *f1 = [Fractionnew];

        Fraction *f2 = [Fractionnew];

//Fraction *ff;

        

        Complex *c1 = [Complexnew];

        Complex *c2 = [Complexnew];

//Complex *cc;

        

//        id dataValue;

//        f1.fenZi = 1;

//        f1.fenMu = 10;

//        dataValue = f1;

//        [dataValue print];

//        

        

        

//        f2.fenZi = 2;

//        f2.fenMu = 15;

//        

//        [f1 print];

//        [f1 add:f2];

//        [f1 reduce];

//        [f1 print];

//        

//        c1.shiBu = 2;

//        c1.xuBu = 3.5;

//        c2.shiBu = 4.3;

//        c2.xuBu = 3;

//        [c1 print];

//        [c2 print];

//        [c1 add:c2];

//        [c1 print];

        

        

    }

    return 0;

}

 

 
Fraction.h

#import <Foundation/Foundation.h>

@interface Fraction : NSObject

@propertyint fenZi, fenMu;

 

-(void)print;

-(void)add: (Fraction *) f;

-(void)reduce;

 

@end

 

 
Fraction.m

#import "Fraction.h"

@implementation Fraction

 

@synthesize fenZi, fenMu;

 

-(void)print{

    NSLog(@"%i/%i",fenZi,fenMu);

}

 

-(void)add: (Fraction *) f{

    

    fenZi = fenZi * f.fenMu + fenMu * f.fenZi;

    fenMu = fenMu * f.fenMu;

}

 

-(void)reduce{

    int u = fenZi;

    int v = fenMu;

    int temp;

    

    while (v != 0) {

        temp = u % v;

        u = v;

        v = temp;

    }

    fenZi /= u;

    fenMu /= u;

}

 

@end

 

 
Complex.h

#import <Foundation/Foundation.h>

@interface Complex : NSObject

 

@propertyfloat shiBu, xuBu;

 

-(void)print;

-(void)add: (Complex *)p;

@end

 

 
Complex.m

 

#import "Complex.h"

@implementation Complex

@synthesize shiBu, xuBu;

 

-(void)print{

    

    NSLog(@"%.2f + %.2f i",shiBu,xuBu);

}

-(void)add: (Complex *)p{

    

    shiBu = shiBu + p.shiBu;

    xuBu = xuBu + p.xuBu;

}

@end

 

多态性

标签:

原文地址:http://www.cnblogs.com/GhostKZShadow/p/5105194.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!