[Objective C] 05 The Id Type

프로그래밍/iPhone Dev 2009. 10. 29. 17:31 Posted by galad
The Id Type

void* 형처럼 움직임.
java나 c++과 다른 것은 object를 통해 method를 호출할 때 object 타입을 알아야만 할 필요가 없음.
단순히 method가 존재하기만 하면 됨.(Objective-C의 메시지 전달 방식)

Fraction.h
#import <Foundation/NSObject.h>

@interface Fraction: NSObject {
    int numerator;
    int denominator;
}

-(Fraction*) initWithNumerator: (int) n andDenominator: (int) d;
-(void) print;
-(void) setNumerator: (int) n;
-(void) setNumerator: (int) n andDenominator: (int) d;
-(void) setDenominator: (int) d;
-(int) numerator;
-(int) denominator;
@end


Fraction.m
#import "Fraction.h"
#import <stdio.h>

@implementation Fraction
-(Fraction*) initWithNumerator: (int) n andDenominator: (int) d {
    self = [super init];
   
    if(self) {
        [self setNumerator: n andDenominator: d];
    }
   
    return self;
}

-(void) print {
    printf( "%i/%i", numerator, denominator );
}

-(void) setNumerator: (int) n {
    numerator = n;
}

-(void) setNumerator: (int) n andDenominator: (int) d {
    numerator = n;
    denominator = d;
}

-(void) setDenominator: (int) d {
    denominator = d;
}

-(int) denominator {
    return denominator;
}

-(int) numerator {
    return numerator;
}
@end


Complex.h
#import <Foundation/NSObject.h>

@interface Complex: NSObject {
    double real;
    double imaginary;
}

-(Complex*) initWithReal: (double) r andImaginary: (double) i;
-(void) setReal: (double) r;
-(void) setImaginary: (double) i;
-(void) setReal: (double) r andImaginary: (double) i;
-(double) real;
-(double) imaginary;
-(void) print;
@end


Complex.m
#import "Complex.h"
#import <stdio.h>

@implementation Complex
-(Complex*) initWithReal: (double) r andImaginary: (double) i {
    self = [super init];
   
    if(self) {
        [self setReal: r andImaginary: i];
    }
   
    return self;
}

-(void) setReal: (double) r {
    real = r;
}

-(void) setImaginary: (double) i {
    imaginary = i;
}

-(void) setReal: (double) r andImaginary: (double) i {
    real = r;
    imaginary = i;
}

-(double) real {
    return real;
}

-(double) imaginary {
    return imaginary;
}

-(void) print {
    printf("%f + %f", real, imaginary);
}
@end


main.m
#import <stdio.h>
#import "Fraction.h"
#import "Complex.h"

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

    // create new instances
    Fraction* frac = [[Fraction alloc] initWithNumerator: 1 andDenominator: 10];
    Complex* comp = [[Complex alloc] initWithReal: 10 andImaginary: 15];
   
    id number;
       
    // print fraction
    number = frac;
    printf( "The fraction is: " );
    [number print];
    printf( "\n" );

    // print complex
    number = comp;
    printf( "The complex number is: " );
    [number print];
    printf( "\n" );

    // free memory
    [frac release];
    [comp release];

    return system("PAUSE");
    return 0;
}


결과
The fraction is: 1/10
The complex number is: 10.000000 + 15.000000
계속하려면 아무 키나 누르십시오 . . .

There are obvious benefits to this type of dynamic binding. You don't have to know the type of something to call a method on it. If the object responds to a message, it will invoke that method. Lots of nasty casting isn't involved in this either, such as in Java to call .intValue() on an integer object would involve casting first, then calling the method.

동적 바인딩의 명백한 장점은 method 호출 시 타입이 뭔지 몰라도 된다는 것과 많은 지저분한 형변환이 필요없다는 거란다.(자바처럼 ㅋ~)
object가 message에 반응하면 method를 실행시킨다는군. 반응 못하면? 예외인가?

'프로그래밍 > iPhone Dev' 카테고리의 다른 글

[Objective C] 07 DynamicTypes  (0) 2009.10.30
[Objective C] 06 Inheritance  (1) 2009.10.30
[Objective C] 04 Exceptions  (0) 2009.10.28
[Objective C] 03 Class Level Access  (0) 2009.10.27
[Objective C] 02 Access Privledges  (0) 2009.10.27