标签:
多态
1.没有继承就没有多态
2.代码的体现:父类类型的指针指向子类对象
3.好处:如果函数\方法参数中使用的是父类类型,可以传入父类、子类对象
4.局限性:
1> 父类类型的变量 不能 直接调用子类特有的方法。必须强转为子类类型变量后,才能直接调用子类特有的方法
@interface Animal : NSObject
- (void)eat;
@end
@implementation Animal
- (void)eat
{
NSLog(@"Animal-吃东西----");
}
@end
// 狗
@interface Dog : Animal
- (void)run;
@end
@implementation Dog
- (void)run
{
NSLog(@"Dog---跑起来");
}
- (void)eat
{
NSLog(@"Dog-吃东西----");
}
@end
// 猫
@interface Cat : Animal
@end
@implementation Cat
- (void)eat
{
NSLog(@"Cat-吃东西----");
}
@end
super的作用
1.直接调用父类中的某个方法
2.super处在对象方法中,那么就会调用父类的对象方法
super处在类方法中,那么就会调用父类的类方法
3.使用场合:子类重写父类的方法时想保留父类的一些行为
#import <Foundation/Foundation.h>
@interface Zoombie : NSObject
-(void)test;
+(void)test;
-(void)wlak;
@end
@implementation Zoombie
-(void)test
{
NSLog(@"调用了-test方法");
}
+(void)test
{
NSLog(@"调用了+test方法");
}
-(void)wlak
{
NSLog(@"挪移挪");
}
@end
@interface JumpZoombie : Zoombie
-(void)haha;
+(void)haha;
@end
@implementation JumpZoombie
-(void)haha
{
[super test];
}
+(void)haha
{
[super test];
}
-(void)wlak
{
NSLog(@"跳一跳");
[super wlak];
}
@end
int main()
{
JumpZoombie *gp = [JumpZoombie new];
[gp haha];
[JumpZoombie haha];
[gp wlak];
return 0;
}
#import <Foundation/Foundation.h>
@interface Student : NSObject
{
NSString *_str;
}
@end
@implementation Student
@end
int mian()
{
NSString *str = @"hahhah";
NSLog(@"我在%@哈啊哈",str);
return 0;
}
标签:
原文地址:http://www.cnblogs.com/iceshan/p/4492867.html