简单的动物园系统实现
#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; }
时间: 2024-10-06 18:44:49