본문 바로가기

C/Objective C/Objective C

Objective C Polymorphism(다형성) 예제 따라하기

- 다형성 – 동일한 이름, 다른 클래스

다형성은 다른 클래스들 간에 동일한 메서드 이름을 사용할 수 있는 기능이다. 다형성을 이용하면 동일한 메서드 이름에 각각 응답할 수 있는 클래스 모음을 개발하는 것이다. 가능하다. 각 클래스 정의에서 특정 한 메서드에 필요한 코드를 캡슐화하여 다른 클래스의 정의로부터 독립적으로 만드는 방식으로 말이다. 또 한 다형성을 통해 동일한 이름의 메서드에 응답할 수 있는 새로운 클래스를 추가할 수도 있다. 

- 소스코드

#import <Foundation/Foundation.h>


@interface Complex : NSObject

double real;

double imaginary; 

}


@property double real, imaginary;


-(void) print;

-(void) setReal: (double) a andImaginary:(double) b; 

-(Complex *) add: (Complex *) f;

@end 

#import "Complex.h"


@implementation Complex 

@synthesize real, imaginary; 

-(void) print

{

NSLog (@" %g + %gi ", real, imaginary); 

}


-(void) setReal: (double) a andImaginary: (double) b {

real = a;

imaginary = b; 

}


-(Complex *) add: (Complex *) f {

Complex *result = [[Complex alloc] init];

[result setReal: real + [f real] andImaginary

imaginary + [f imaginary]]; return result;

}

@end 

#import <Foundation/Foundation.h>


@interface Fraction : NSObject

int numerator;

int denominator; 

}


@property int numerator, denominator;


-(void) print;

-(void) setTo: (int) n over: (int) d; 

-(double) convertToNum;

-(void) reduce;

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


@end 

#import "Fraction.h"


@implementation Fraction


@synthesize numerator, denominator;


-(void) print {

NSLog (@"%i/%i", numerator, denominator); }

-(double) convertToNum {

if (denominator != 0)

return (double) numerator / denominator;

else

return 1.0;

}

-(void) setTo: (int) n over: (int) d {

numerator = n;

denominator = d; 

}

-(void) reduce {

int u = numerator; int v = denominator; int temp;

while (v != 0) { temp = u % v;

u = v;

v = temp; 

}

numerator /= u;

denominator /= u; 

}

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

// To add two fractions:

// a/b + c/d = ((a*d) + (b*c) / (b * d)

// result will store the result of the addition 

Fraction *result = [[Fraction alloc] init];

int resultNum, resultDenom;

resultNum = numerator * f.denominator + denominator * f.numerator

resultDenom = denominator * f.denominator;

[result setTo: resultNum over: resultDenom]; 

[result reduce];

return result; 

}

@end 

#import <Foundation/Foundation.h>

#import "Fraction.h" 

#import "Complex.h"


int main(int argc, const char * argv[])

{


Fraction *f1 = [[Fraction alloc] init]; 

Fraction *f2 = [[Fraction alloc] init]; 

Fraction *fracResult;

Complex *c1 = [[Complex alloc] init]; 

Complex *c2 = [[Complex alloc] init]; 

Complex *compResult;

[f1 setTo: 1 over: 10]; 

[f2 setTo: 2 over: 15];

[c1 setReal: 18.0 andImaginary: 2.5]; 

[c2 setReal: -5.0 andImaginary: 3.2];

// add and print 2 complex numbers

[c1 print]; 

NSLog (@" ---------"); 

   

compResult = [c1 add: c2]; 

[compResult print];

NSLog (@"\n");

[c2 print];

// add and print 2 fractions

[f1 print]; 

NSLog (@" +"); 

[f2 print]; 

NSLog (@"----");

fracResult = [f1 add: f2];

[fracResult print];

    return 0;

} 


- 실행화면