標籤:
簡單的動物園系統實現
#import <Foundation/Foundation.h>#import "Animal.h"//動物園@interface Zoo : NSObject@property (nonatomic, strong) Animal *animal;//動物@property (nonatomic, assign) int foodSave;//食物儲備/** * 初始化方法 傳入儲備的食物數量 */- (id)initWithFoodSave:(int)foodSave;@end
#import "Zoo.h"@implementation Zoo- (id)initWithFoodSave:(int)foodSave { if (self = [super init]) { _foodSave = foodSave; } return self;}@end
動物園中有各種動物,以下代碼構建動物類,以繼承方式實現:
#import <Foundation/Foundation.h>@interface Animal : NSObject@property (nonatomic, assign) int count;//數量@property (nonatomic, assign) int eatFood;//食量@property (nonatomic, assign) int productionCycle;//生產周期//自訂初始化 傳入3個參數:1.當前動物數量 2.當前動物每天吃的食物數量 3.生產周期為多少天- (id)initWithCount:(int)cout andEatFood:(int)eatFood andProductionCycle:(int)productionCycle;//吃東西- (int)animalEatFood;//生寶寶- (int)productionBaby;@end#import "Animal.h"@implementation Animal- (id)initWithCount:(int)cout andEatFood:(int)eatFood andProductionCycle:(int)productionCycle { if (self = [super init]) { self.count = cout; self.eatFood = eatFood; self.productionCycle = productionCycle; } return self;}//吃東西- (int)animalEatFood { NSLog(@"吃東西了!!!! %d %d", self.eatFood, self.count); return self.eatFood * self.count;}//生寶寶- (int)productionBaby { return 0;}@end
#import "Animal.h"@interface Panda : Animal@end#import "Panda.h"@implementation Panda//重寫父類方法- (int)animalEatFood { NSLog(@"熊貓吃掉 %d 個單位的食物, 現有 %d 只", self.eatFood * self.count, self.count); return self.eatFood * self.count;}- (int)productionBaby { NSLog(@"熊貓生產了 3 個寶寶"); self.count += 3; return 3;}@end#import "Animal.h"@interface Elephant : Animal@end#import "Elephant.h"@implementation Elephant- (int)animalEatFood { NSLog(@"大象吃掉 %d 個單位的食物, 現有 %d 只", self.eatFood * self.count, self.count); return self.eatFood * self.count;}- (int)productionBaby { NSLog(@"大象生產了 1 個寶寶"); self.count += 1; return 1;}@end#import "Animal.h"@interface Kangaroo : Animal@end#import "Kangaroo.h"@implementation Kangaroo- (int)animalEatFood { NSLog(@"袋鼠吃掉 %d 個單位的食物, 現有 %d 只", self.eatFood * self.count, self.count); return self.eatFood * self.count;}- (int)productionBaby { NSLog(@"袋鼠生產了 2 個寶寶"); self.count += 2; return 2;}@end
以下是main函數中的測試代碼:
#import <Foundation/Foundation.h>#import "Zoo.h"#import "Panda.h"#import "Elephant.h"#import "Kangaroo.h"int main(int argc, const char * argv[]) { @autoreleasepool { //建立動物員對象 並對儲存的食物進行初始化 Zoo *zoo = [[Zoo alloc] initWithFoodSave:1000]; //初始化各種動物資訊 (運用多態) Animal *panda = [[Panda alloc] initWithCount:2 andEatFood:1 andProductionCycle:3]; Animal *elephant = [[Elephant alloc] initWithCount:2 andEatFood:5 andProductionCycle:5]; Animal *kangaroo = [[Kangaroo alloc] initWithCount:2 andEatFood:2 andProductionCycle:4]; int day = 1; while (zoo.foodSave > 0) { //到了動物的生產周期時,動物開始生產 if (day % panda.productionCycle == 0) { [panda productionBaby]; } if (day % elephant.productionCycle == 0) { [elephant productionBaby]; } if (day % kangaroo.productionCycle == 0) { [kangaroo productionBaby]; } int usedFood = [panda animalEatFood] + [elephant animalEatFood] + [kangaroo animalEatFood]; zoo.foodSave -= usedFood; NSLog(@"第%d天,剩餘食物%d", day++, zoo.foodSave); //如果食物已經不足動物使用一天,那就必須要採購了,此時應該不再繼續,因此讓食物為0 if (zoo.foodSave < usedFood) { zoo.foodSave = 0; } } } return 0;}
Objective-C文法應用