Object-C 中的虚函数的学习记录

小明小李小王继承Person


#import "Person.h"

@implementation Person

- (void)runAway{
    NSLog(@"run");
    return;
}

#import "xiaoming.h"
#import "Person.h"
@implementation xiaoming

- (void)runAway{
    NSLog(@"xiaoming can run 15m/s");
    return;
}

#import "xiaoli.h"

@implementation xiaoli

- (void)runAway{
    NSLog(@"xiaoli can run 12m/s");
    return;
}

#import "xiaowang.h"

@implementation xiaowang

- (void)runAway{
    NSLog(@"xiaowang can run 10m/s");
    return;
}




#import "Person.h"

@interface Teacher : NSObject

- (void)amHere:(Person *)p;

@end

#import "Teacher.h"
#import "Person.h"
@implementation Teacher

- (void)amHere:(Person *)p{
    
    NSLog(@"i am %@",[p class]);
    
    [p runAway];
    
    return;
}

@end



//OC 调用方法不看指针,看对象(地址),对象的地址调用对象的方法
    //父类的指针可以指向子类的对象,同一事件在不同的事物上,可以产生不同的结果
    
    xiaoming *ming = [[xiaoming alloc]init];
    xiaoli *li = [[xiaoli alloc]init];
    xiaowang *wang = [[xiaowang alloc]init];
    
    Teacher *t = [[Teacher alloc]init];
    
    [t amHere:ming];
    [t amHere:li];
    [t amHere:wang];

结果:Object-C 中的虚函数的学习记录

Object-C 中的虚函数的学习记录

上一篇:服务器端沾包处理的copy效率问题


下一篇:poj3259