两个类:
@interface MyClass : NSObject{}
- (void)show;
@end
@implementation MyClass
- (void)show {
NSLog(@"MyClass");
}
@end
@interface MySubClass : MyClass
{}
@end
@implementation MySubClass
- (void)show {
NSLog(@"MySubClass");
}
@end
一个临时函数:
- (void)show:(void *)temp {
MyClass *mc = (MyClass *)temp;
[mc show];
}
在主函数中调用:
MySubClass *msc = [[MySubClass alloc] init];
void *temp = (void *)msc;
[self show:temp];
控制台的结果:
MySubClass
本文通过一个简单的Objective-C示例介绍了类继承的概念及其实现方式,演示了父类和子类之间的关系,并展示了如何通过多态实现不同类实例间相同方法的不同表现。
394

被折叠的 条评论
为什么被折叠?



