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