20150617_OC之父类指针指向子类对象

//
//  Animal.h
//  IOS150617_ObjectiveC_父类指针指向子类对象
//
//  Created by qianfeng on 15/6/17.
//  Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Animal : NSObject
{
    NSString    *_name;
    NSInteger    _age;
}

@property (copy,nonatomic)NSString *name;
@property (assign ,nonatomic)NSInteger age;

- (void)printAnimalFood;
@end

<pre name="code" class="objc">//  Animal.m
//  IOS150617_ObjectiveC_父类指针指向子类对象
//
//  Created by qianfeng on 15/6/17.
//  Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

#import "Animal.h"

@implementation Animal

- (void)printAnimalFood
{
    
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"name = %@,age = %li",[self name],[self age]];
}

@end

//  Cat.h
//  IOS150617_ObjectiveC_父类指针指向子类对象
//
//  Created by qianfeng on 15/6/17.
//  Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

#import "Animal.h"

@interface Cat : Animal
{
    NSString *_colorOfCat;
}
@property (copy,nonatomic)NSString *colorOfCat;
@end

//  Cat.m
//  IOS150617_ObjectiveC_父类指针指向子类对象
//
//  Created by qianfeng on 15/6/17.
//  Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

#import "Cat.h"

@implementation Cat
- (void)printAnimalFood
{
    NSLog(@"Cat like to eat fish");
}

- (NSString *)description
{
    return  [NSString stringWithFormat:@"name = %@,age = %li,color = %@",_name,_age,self->_colorOfCat];
}
@end

//  Dog.h
//  IOS150617_ObjectiveC_父类指针指向子类对象
//
//  Created by qianfeng on 15/6/17.
//  Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

#import "Animal.h"

@interface Dog : Animal
{
    float _weight;
}
@property (assign,nonatomic)float weight;
@end


 
<pre name="code" class="objc">//
//  Dog.m
//  IOS150617_ObjectiveC_父类指针指向子类对象
//
//  Created by qianfeng on 15/6/17.
//  Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

#import "Dog.h"

@implementation Dog

- (void)printAnimalFood
{
    NSLog(@"Dog like to eat bone");
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"name = %@,age = %li,weight = %.2f",_name,_age,_weight];
}
@end


 
<pre name="code" class="objc">//  main.m
//  IOS150617_ObjectiveC_父类指针指向子类对象
//
//  Created by qianfeng on 15/6/17.
//  Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Cat.h"
#import "Dog.h"

//多态,父类指针指向子类对象,
void printInfo(Animal *animal)
{
    [animal printAnimalFood];
    NSLog(@"%@",animal.description);
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Dog *dog = [[Dog alloc]init];
        dog.name = @"狗";
        dog.age  = 12;
        dog.weight = 12.34;
        [dog printAnimalFood];
        NSLog(@"%@",dog.description);
        
        //父类的对象指针指向子类的对象
        //调用的方法看具体对象的类型,先从子类查找对应方法的实现,子类没有实现对应的方法,跳到父类查找对应的方法实现,父类没有实现对应的方法,再继续向上追踪到父类的父类
        Animal *dogAnimal = dog;
        [dogAnimal printAnimalFood];
        NSLog(@"%@",dogAnimal.description);
    
        Cat *cat = [[Cat alloc] init];
        cat.name = @"猫";
        cat.age  = 10;
        cat.colorOfCat = @"白色";
        [cat printAnimalFood];
        NSLog(@"%@",[cat description]);
        
        Animal *catAnimal = cat;
        [catAnimal printAnimalFood];
        NSLog(@"%@",catAnimal.description);
        
        NSLog(@"==========多态=========");
        //多态,根据不同的对象,打印不同的结果
        printInfo(cat);
        printInfo(dog);
    }
    return 0;
}


 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值