oc3-the parent class pointer to the child class object

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.