Objective-C文法應用

來源:互聯網
上載者:User

標籤:

簡單的動物園系統實現

#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文法應用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.