微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Cocoa 接口的定义于检测

 

//-------接口定义

@protocol Printing

-(void) print;

 

@end

---------

 

//----实现接口 Printing -------

//----接口中的print 方法不必写出来

@interface Fraction : NSObject <Printing,NScopying> {

int numerator;

    int denominator;

}

-(Fraction*) initWithNumerator:(int)n denominator:(int)d;

-(int)  numerator;

-(int)  denominator;

-(void) setNumerator:(int)n;

-(void) setDenominator:(int)n;

-(void) setNumeratorAndDenominator:(int) num 

  andDeno:(int) den;

 

@end

--------------

@interface Complex : NSObject<Printing>{

double real;

double image;

}

 

-(Complex*) initWithReal: (double) r andImage: (double) i;

-(void)   setReal: (double) r;

-(void)   setimage: (double) i;

-(void)   setReal: (double) r andImage: (double) i;

-(double) real;

-(double) image;

 

@end

--------------

Fraction *frac = [[Fraction alloc] initWithNumerator: 3 denominator: 10];

    Complex *comp = [[Complex alloc] initWithReal: 5 andImage: 15];

    id <Printing> printable;

 

    copyPrintable = frac;

  

    // true

    if ( [frac conformsToProtocol: @protocol( NScopying )] == YES ) {

        printf( "Fraction conforms to NScopying/n" );

    }

   if ( [frac conformsToProtocol: @protocol( Printing )] == YES ) {

        printf( "Fraction conforms to Printing/n" );

    }

    // false

    if ( [comp conformsToProtocol: @protocol( NScopying )] == YES ) {

        printf( "Complex conforms to NScopying/n" );

    }

    //true

    if ( [comp conformsToProtocol: @protocol( Printing )] == YES ) {

        printf( "Complex conforms to  Printing/n" );

    }

    // free memory

    [frac release];

    [comp release];

 

============接口的定义于实现

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐