OBJECTIVE-C Syntax Application

Source: Internet
Author: User

Simple Zoo System implementation

#import<Foundation/Foundation.h>#import "Animal.h"//Zoo@interfaceZoo:nsobject@property (nonatomic, strong) Animal*animal;//Animal@property (nonatomic, assign)intFoodsave;//Food Storage/** * Initialize Method The quantity of food passed in the reserve*/- (ID) Initwithfoodsave: (int) Foodsave;@end
#import " Zoo.h " @implementation Zoo-(ID) Initwithfoodsave: (int) foodsave {    if (self = [super Init]) {        = foodsave;    }     return Self ;} @end

There are various animals in the zoo, the following code constructs the animal class, implements by the inheritance way:

#import<Foundation/Foundation.h>@interfaceAnimal:nsobject@property (nonatomic, assign)intCount//Quantity@property (nonatomic, assign)intEatfood;//Food Intake@property (nonatomic, assign)intProductioncycle;//Production Cycle//Custom Initialization Incoming 3 parameters: 1. The current number of animals 2. The amount of food that the animal eats every day 3. The production cycle is the number of days- (ID) Initwithcount: (int) cout Andeatfood: (int) Eatfood Andproductioncycle: (int) productioncycle;//Eat something- (int) Animaleatfood;//Raw Baby- (int) Productionbaby;@end#import "Animal.h"@implementationAnimal- (ID) Initwithcount: (int) cout Andeatfood: (int) Eatfood Andproductioncycle: (int) productioncycle {if(self =[Super Init]) {Self.count=cout; Self.eatfood=Eatfood; Self.productioncycle=productioncycle; }    returnSelf ;}//Eat something- (int) Animaleatfood {NSLog (@"I ate.!!!!%d%d", Self.eatfood, Self.count); returnSelf.eatfood *Self.count;}//Raw Baby- (int) Productionbaby {return 0;}@end
#import "Animal.h"@interfacePanda:animal@end#import "Panda.h"@implementationPanda//overriding the parent class method- (int) Animaleatfood {NSLog (@"Pandas eat%d units of food, existing%d", Self.eatfood *Self.count, Self.count); returnSelf.eatfood *Self.count;}- (int) Productionbaby {NSLog (@"the panda has produced 3 babies."); Self.count+=3; return 3;}@end#import "Animal.h"@interfaceElephant:animal@end#import "Elephant.h"@implementationElephant- (int) Animaleatfood {NSLog (@"Elephants eat%d units of food, existing%d", Self.eatfood *Self.count, Self.count); returnSelf.eatfood *Self.count;}- (int) Productionbaby {NSLog (@"the elephant produced 1 babies."); Self.count+=1; return 1;}@end#import "Animal.h"@interfaceKangaroo:animal@end#import "Kangaroo.h"@implementationKangaroo- (int) Animaleatfood {NSLog (@"Kangaroo eats%d units of food, existing%d", Self.eatfood *Self.count, Self.count); returnSelf.eatfood *Self.count;}- (int) Productionbaby {NSLog (@"Kangaroo produces 2 babies."); Self.count+=2; return 2;}@end

The following is the test code in the main function:

#import<Foundation/Foundation.h>#import "Zoo.h"#import "Panda.h"#import "Elephant.h"#import "Kangaroo.h"intMainintargcConst Char*argv[]) {@autoreleasepool {//Create an Animal object and initialize the stored foodZoo *zoo = [[Zoo alloc] Initwithfoodsave: +]; //initializing various animal information (using polymorphism)Animal *panda = [[Panda alloc] Initwithcount:2Andeatfood:1Andproductioncycle:3]; Animal*elephant = [[Elephant alloc] Initwithcount:2Andeatfood:5Andproductioncycle:5]; Animal*kangaroo = [[Kangaroo alloc] Initwithcount:2Andeatfood:2Andproductioncycle:4]; intDay =1;  while(Zoo.foodsave >0) {            //when the animal's production cycle begins, the animals begin to produce            if(day% Panda.productioncycle = =0) {[Panda Productionbaby]; }            if(day% Elephant.productioncycle = =0) {[Elephant Productionbaby]; }            if(day% Kangaroo.productioncycle = =0) {[Kangaroo productionbaby]; }                        intUsedfood = [Panda Animaleatfood] + [elephant Animaleatfood] +[Kangaroo Animaleatfood]; Zoo.foodsave-=Usedfood; NSLog (@"%d days, remaining food%d", day++, Zoo.foodsave); //If the food is not enough for the animal to use for one day, it must be purchased and should not continue at this time, so let food be 0            if(Zoo.foodsave <Usedfood) {Zoo.foodsave=0; }        }    }    return 0;}

OBJECTIVE-C Syntax Application

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.