码迷,mamicode.com
首页 > 编程语言 > 详细

20-黑马程序员------OC 语言学习笔记---多态

时间:2015-05-10 20:23:00      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

多态

 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;
    
    
    
}

  

20-黑马程序员------OC 语言学习笔记---多态

标签:

原文地址:http://www.cnblogs.com/iceshan/p/4492867.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!