动态绑定和id类型
#import <Foundation/Foundation.h>
#import "MyClassA.h"
#import "MyClassB.h"
// 动态绑定和id类型
//多态形式: id指向子类,打印子类对应的方法
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
id c = [[MyClassA alloc]init];
[c print];
c = [[MyClassB alloc]init];
[c print];
}
return 0;
}
#import <Cocoa/Cocoa.h>
@interface MyClassA : NSObject
-(void)print;
@end
#import "MyClassA.h"
@implementation MyClassA
-(void)print
{
NSLog(@"MyClassA");
}
@end
#import <Cocoa/Cocoa.h>
@interface MyClassB : NSObject
-(void)print;
@end
#import "MyClassB.h"
@implementation MyClassB
-(void)print
{
NSLog(@"MyClassB");
}
@end