////Cat.h//oc3-the parent class pointer to the child class object////Created by Qianfeng on 15/6/17.//Copyright (c) 2015 Qianfeng. All rights reserved.//#import "Animal.h"@interfacecat:animal{float_height;} @property (assign,nonatomic)floatheight;@end////CAT.M//oc3-the parent class pointer to the child class object////Created by Qianfeng on 15/6/17.//Copyright (c) 2015 Qianfeng. All rights reserved.//#import "Cat.h"@implementationCat-(void) printanimalfood{NSLog (@"cat like to eat fish");}-(NSString *) description{return[NSString stringWithFormat:@"name=%@,age=%li,height=%.2f", _name,_age,_height];}@end
////Dog.h//oc3-the parent class pointer to the child class object////Created by Qianfeng on 15/6/17.//Copyright (c) 2015 Qianfeng. All rights reserved.//#import "Animal.h"@interfacedog:animal{float_weight;}-(void) Printanimalhaha; @property (assign,nonatomic)floatweight;@end////DOG.M//oc3-the parent class pointer to the child class object////Created by Qianfeng on 15/6/17.//Copyright (c) 2015 Qianfeng. All rights reserved.//#import "Dog.h"@implementationDog-(void) printanimalfood{NSLog (@"dog like to eat bone");}//-(NSString *) Description//{//return [NSString stringwithformat:@ "name=%@,age=%li,weight=%.2f", _name,_age,_weight];//}-(void) printanimalhaha{NSLog (@"HaHa");}@end
////Animal.h//oc3-the parent class pointer to the child class object////Created by Qianfeng on 15/6/17.//Copyright (c) 2015 Qianfeng. All rights reserved.//#import<Foundation/Foundation.h>@interfaceanimal:nsobject{NSString*_name; Nsinteger _age;} @property (copy,nonatomic) NSString*Name: @property (assign,nonatomic) Nsinteger age;-(void) Printanimalfood;@end////ANIMAL.M//oc3-the parent class pointer to the child class object////Created by Qianfeng on 15/6/17.//Copyright (c) 2015 Qianfeng. All rights reserved.//#import "Animal.h"@implementationAnimal-(void) printanimalfood{NSLog (@"-----Animal");}-(NSString *) description{return[NSString stringWithFormat:@"Name111=%@,age=%li", [self name],self.age];}@end
////main.m//oc3-the parent class pointer to the child class object////Created by Qianfeng on 15/6/17.//Copyright (c) 2015 Qianfeng. All rights reserved.//#import<Foundation/Foundation.h>#import "Dog.h"#import "Cat.h"#import "Animal.h"voidShowanimalfood (Animal *ANI) {[Ani printanimalfood];}intMainintargcConst Char*argv[]) {@autoreleasepool {Dog*xiaohei =[[Dog alloc] init]; Xiaohei.name=@"Xiaogei"; Xiaohei.age= A; Xiaohei.weight=12.6; //[Xiaohei description];NSLog (@"%@", Xiaohei); Cat*xiaomao =[[Cat alloc] init]; Xiaomao.name=@"Xiaomao"; Xiaomao.age= A; Xiaomao.height=12.6; //[Xiaohei description];NSLog (@"%@", Xiaomao); //The object pointer of the parent class can point to the object of the child class//call the method to see the specific object type, first from the subclass to find the corresponding method implementation, if the subclass does not implement the corresponding method, jump to the parent class to find the corresponding method implementation, if the parent class does not implement the corresponding method, and then jump to the parent class to find the corresponding method implementation.Animal *ani =Xiaohei; [Ani Printanimalfood]; Ani=Xiaomao; [Ani Printanimalfood]; Showanimalfood (Xiaomao); Showanimalfood (Xiaohei); //different responses are made according to the different objects that are passed in. polymorphic } return 0;}
oc3-the parent class pointer to the child class object