iOS Development Zoo Management inheritance polymorphism implementation

Source: Internet
Author: User

////main.m//inherit////#import <Foundation/Foundation.h> #import "Animal.h" #import "Cat.h" #import "Dog.h" #import "FeedMan.h" int main (int argc, const char * argv[]) {//Animal * Animal = [Animal new];        [Animal eat];////[Animal sleep];        Forgot to introduce header file//Animal * Cat =[[cat alloc]init];////[cat eat]; Cat * cat = [[Cat alloc]init];////[Cat catchmouse];////dog * dog = [[] Dog alloc]init];////[Do    G bark];////Parent class pointer holds the subclass object, how can I invoke the method of the child class object? Animal * Animal_cat = [[Cat alloc]init];////Feedman * man = [[Feedman alloc]init];//]/[Man showname:        ANIMAL_CAT];    [Animal_cat eat];        [Animal_cat setname:@ "Hello Cat"];        Animal * Animal_dog = [[Dog alloc]init];        Feedman * man = [[Feedman alloc]init];        [Man Showname:animal_dog];        [Man Feedanimal:animal_dog];            Subclass calls the parent class method, how to implement the different nature of the method? return 0;} 

  feedman.h//  Inheritance//#import "Animal.h" @interface feedman:nsobject-(void) ShowName: (Animal *) animal;-(void ) Feedanimal: (Animal *) Animal; @end

  feedman.m//  inherit #import "FeedMan.h" #import "Dog.h" #import "Cat.h" @implementation feedman-(void) Feedanimal: (Animal *) animal{    if ([Animal Iskindofclass:[dog class]]) {                dog * dog = (dog *) Animal;        [Dog eat];    }} -(void) ShowName: (Animal *) animal{    //can dynamically detect the type of animal used to a class?    if ([Animal Iskindofclass:[dog class]])    {        //need to force type conversion        Dog * dog = (dog *) animal;        [Dog bark];    }    else if ([Animal Iskindofclass:[cat class]])    {        Cat * cat = (CAT *) animal;        [Cat Catchmouse];    }    } @end

  animal.h//  inherit #import <Foundation/Foundation.h> @interface animal:nsobject{    nsstring * _name;        int _age;} @property NSString * name; @property int age;-(void) eat;-(void) sleep;-(void) showage; @end

  animal.m//  inherit//#import "Animal.h" @implementation animal-(void) eat{    NSLog (@ "Animal Eats");} -(void) sleep{    NSLog (@ "The animal Sleeps"); -(void) showage{    NSLog (@ "Age of small animals"); @end

  dog.h//  inherit//#import "Animal.h" @interface dog:animal{    }-(void) bark;-(void) eat; @end

  dog.m//  Inheritance//#import "Dog.h" @implementation dog-(void) bark{    NSLog (@ "Puppy barking");} -(void) eat{    NSLog (@ "Puppy Eats");} @end

  cat.h//  inherit #import "Animal.h" @interface cat:animal{    }-(void) catchmouse;-(void) eat; @end

  cat.m//  Inheritance//#import "Cat.h" @implementation cat{    }-(void) catchmouse{    NSLog (@ "Cats catch mice!" ");} -(void) eat{    NSLog (@ "Kitten eats small fish"); @end


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

iOS Development Zoo Management inheritance polymorphism implementation

Related Article

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.