IOS polymorphism and dynamic binding

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Polymorphism and dynamic binding is to solve the superclass subclass of call   

  First of all, to declare three Class AA BB CC inherits from the fist class

#import <Foundation/Foundation.h>

@interface fist : NSObject

{

    SEL name;

}

-(void)print;

@end

@implementation fist

-(void)print

{

    NSLog(@"I'm AA");

}

@end

 

@interface aa : fist

-(void)nihao;

@end

@implementation aa

 

-(void)nihao

{

    NSLog(@"buhdddddddddao");

}

 

@end

 

 

@interface bb : fist

-(void)nihao;

@end

@implementation bb

 

-(void)nihao

{

    NSLog(@"I'm BB");

}

 

@end

@interface cc : aa

-(void)nihao;

@end

@implementation cc

 

-(void)nihao

{

    NSLog(@"I'm CC");

}

 

@end

 

 

 

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

{

 

    @autoreleasepool {

aa *a=[[aa alloc] init];//Object declarations of AA

bb *b=[[bb alloc] init]; //'''

cc *c=[[cc alloc] init];

fist *dd[3]={a,b,c};//Can use the   class ID or NSobject;

  for (int i=0; i<3; i++) {

 

SEL gong;

gong=@selector(nihao);//A selector

if ([dd[i] respondsToSelector:gong]==YES) {//Traversing.

[dd[i] performSelector:gong];

[dd[i] release];

     }

}

}

 

    }

    return 0;

}


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Ada at December 02, 2013 - 9:40 PM