- #import <Foundation/Foundation.h>
- @interface Person:NSObject{
-
- }
- -(void)run;
- @end
- @implementation Person
- -(void)run{
- NSLog(@"人跑了~~");
- }
- @end
- @interface Police : Person
- -(void)run;
- @end
- @implementation Police
- -(void)run{
- [super run];
- NSLog(@"警察开摩托车跑了~~");
- }
- @end
- @interface PoliceMaster : Police
- -(void)run;
- @end
- @implementation PoliceMaster
- -(void)run{
- //调用Person方法
- Person *p = [[Person alloc]init];
- [p run];
- NSLog(@"警察局长开汽车跑了~~");
- }
- @end
- int main(int argc, const char * argv[]) {
- @autoreleasepool {
- //调用Person方法
- Person *p = [Person new];
- [p run];
- printf("--------\n");
- //调用Police方法,Police调用父类的方法并追加信息
- p = [Police new];
- [p run];
- printf("--------\n");
- //调用PoliceMaster方法
- p = [PoliceMaster new];
- [p run];
-
- }
- return 0;
- }
复制代码
super只能访问父类,如果你要在PoliceMaster中访问Person的run方法,只能通过实例化的方式调用 |