多态和动态绑定是为了解决父类调用子类的问题
首先,声明三个类aa bb cc 都继承于fist类
#import <Foundation/Foundation.h>
@interface fist : NSObject
{
SEL name;
}
-(void)print;
@end
@implementation fist
-(void)print
{
NSLog(@"我是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(@"我是bb");
}
@end
@interface cc : aa
-(void)nihao;
@end
@implementation cc
-(void)nihao
{
NSLog(@"我是cc");
}
@end
int main(int argc, const char * argv[])
{
@autoreleasepool {
aa *a=[[aa alloc] init];//声明aa的对象
bb *b=[[bb alloc] init]; //'''
cc *c=[[cc alloc] init];
fist *dd[3]={a,b,c};//可以用父类 id 或者NSobject
for (int i=0; i<3; i++) {
SEL gong;
gong=@selector(nihao);//定一个选择器
if ([dd[i] respondsToSelector:gong]==YES) {//逐个遍历出来
[dd[i] performSelector:gong];
[dd[i] release];
}
}
}
}
return 0;
}