标签:style io ar for sp strong on log cti
Main.m
#import "Panda.h"
#import "Elephant.h"
#import "Kangaroo.h"
#import "Zoo.h"
#import "Admin.h"
int main(int argc, const char * argv[])
{
@autoreleasepool
{
//创建对象
Panda *panda = [[[Panda alloc] initWithName:@"熊猫"]autorelease];
Elephant *elephant = [[[Elephant alloc] initWithName:@"大象"]autorelease];
Kangaroo *kangaroo = [[[Kangaroo alloc] initWithName:@"袋鼠"]autorelease];
Admin *admin = [[[Admin alloc]init]autorelease];
Zoo *zoo = [[[Zoo alloc] init]autorelease];
//设置仓库的总量
zoo.houseSize = 500;
//设置饲料的总量
zoo.store =zoo.houseSize;
//添加动物到数组中
[zoo addAnimal:panda];
[zoo addAnimal:elephant];
[zoo addAnimal:kangaroo];
//动物园营业了
[zoo run:admin];
//定时器,代码暂停
[[NSRunLoop currentRunLoop] run];
}
return 0;
}@class Zoo;
@class Animal;
@class Admin;
//动物管理协议
@protocol helpAnimal <NSObject>
@required
//为动物园购买饲料
- (void)buyFodder:(Zoo *)zoo;
@optional
//有新动物出生,照顾新动物
- (void)lookAfterAnimal:(Animal *)animal;
@end
//主体
@interface Zoo : NSObject
{
//动物园的动物
NSMutableArray *_animals;
//饲料的总量
NSInteger _store;
//营业的天数
NSInteger _days;
//仓库总量
NSInteger _houseSize;
}
@property(nonatomic, assign)NSInteger houseSize;//仓库总量
@property(nonatomic, assign)NSInteger store;//饲料总量
//添加动物到数组
- (void)addAnimal:(Animal *)animal;
//动物园开始运营
- (void)run:(Admin *)admin;
#import "Animal.h"
#import "Admin.h"
@implementation Zoo
{
NSInteger code; //动物编码
}
- (id)init
{
self = [super init];
if (self)
{
_days = 1;//营业天数初始化
// ★ _animals = [[NSMutableArray array] retain];★
//或者
_animals = [[NSMutableArray alloc] init];
code = 100;//动物编码初始化
}
return self;
}
- (void)addAnimal:(Animal *)animal
{
//给添加动物到数组
[_animals addObject:animal];
}
//动物园开始运营
- (void)run:(Admin *)admin
{
//创建信息管理员
admin.name=@"老王";
NSLog(@"动物园开始营业了,%@上班了",admin.name);
//开启定时器
[NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(timeAction:)
userInfo:admin//传送信息
repeats:YES];
}
- (void)timeAction:(NSTimer *)time
{
//接收信息管理员
Admin *admin = [time userInfo];
//---------创建一个可变数组【新动物数组】-----------
NSMutableArray *newanimal = [[[NSMutableArray alloc] init] autorelease];
//---------不可变数组Animal的遍历---------------
//每个动物拿出来遍历
for (Animal *animal in _animals)
{
if (_store <= 0)
{
break;
}
//总量减去每个动物的食量(一个一个吃)
_store -= animal.eating;
//动物的年龄增加
animal.age ++;
//判断动物是否在生产周期
if (animal.age % animal.birthCycle == 0 && animal.age > 0)
{
//------字符串的拼接-------
NSString *name = [NSString stringWithFormat:@"%ld",code];
//调用生产方法,生产小动物并给它带编码的名字
Animal *baby = [animal birth:name];
//错误,_animals正在循环遍历,不允许修改
//[_animals addObject:baby];
//管理员照顾新生的小动物
[admin lookAfterAnimal:animal];
//----------给数组添加元素----------
//将新动物添加到新动物数组中
[newanimal addObject:baby];
code ++;//编码往后累加
}
}
//----------数组元素个数--------
//如果新动物的数组中有元素,那么将新动物数组中的元素添加到动物数组中
if (newanimal.count > 0)
{
//---------将数组元素添加到数组中(复制A数组全部元素到B数组
[_animals addObjectsFromArray:newanimal];
}
NSLog(@"第%ld天、动物的数量:%ld、动物园饲料的余量:%ld",_days,_animals.count,_store);
_days ++;//营业天数增加
if (_store <= 0)
{
NSLog(@"动物园的饲料没有了,请及时添加");
//--------关闭定时器--------------
// [time invalidate];
//管理员为动物园去买饲料
//--------方法调用类自己本身--------
[admin buyFodder:self];
}
}
//销毁
- (void)dealloc
{
//清除动物
[_animals release];
[super dealloc];
}
#import "Zoo.h"//拿到协议
@interface Admin : NSObject<helpAnimal>//签协议
{
NSString *_name;
}
@property(nonatomic,copy)NSString *name;#import "Animal.h"
#import "Zoo.h"
@implementation Admin
//实现协议内容
//购买饲料
- (void)buyFodder:(Zoo *)zoo
{
NSLog(@"管理员%@去购买饲料",_name);
//给仓库饲料加满
zoo.store = zoo.houseSize;
}
//有新动物出生,照顾新动物
- (void)lookAfterAnimal:(Animal *)animal
{
NSLog(@"管理员照顾新动物");
}{
NSString *_name;//动物的名字
}
//自定义初始化(带名字)
- (id)initWithName:(NSString *)name;
//食量
@property(nonatomic, assign) int eating;
//生产周期
@property(nonatomic, assign) int birthCycle;
//年龄
@property(nonatomic, assign) int age;
//生产的方法【带一个参数:编码】
- (Animal *)birth:(NSString *)code;
//----------自定义初始化方法的重写(父)--------
- (id)initWithName:(NSString *)name
{
self = [super init];
if (self)
{
//------给字符串retain-------
_name = [name copy];
}
return self;
}
//----------生产方法的重写(父)--------
//【带一个参数:编码】
- (Animal *)birth:(NSString *)code
{
return nil;
}Panda.m
//----------自定义初始化方法的重写(子)--------
- (id)initWithName:(NSString *)name
{
//--------调用父类的方法---------
self = [super initWithName:name];
if (self)
{
self.eating = 10; //初始化食量
self.birthCycle = 5; //生产周期
self.age = 0; //年龄
}
return self;
}
//--------生产方法的重写(子)---------
- (Animal *)birth:(NSString *)code
{
//给新动物编号
NSString *name = [NSString stringWithFormat:@"熊猫%@",code];
//创建小熊猫
Panda *panda = [[[Panda alloc] initWithName:name] autorelease];
NSLog(@"熊猫生产了一个小熊猫,名字:%@",name);
return panda;
}
Elephant.m
//----------自定义初始化方法的重写(子)--------
- (id)initWithName:(NSString *)name
{
self = [super initWithName:name];
if (self)
{
self.eating = 15; //初始化食量
self.birthCycle = 10; //生产周期
self.age = 0;
}
return self;
}
//--------生产方法的重写(子)---------
- (Animal *)birth:(NSString *)code
{
NSString *name = [NSString stringWithFormat:@"大象%@",code];
Elephant *elephant = [[[Elephant alloc] initWithName:name] autorelease];
NSLog(@"大象生产了一个小象,名字:%@",name);
return elephant;
}
//----------自定义初始化方法的重写(子)--------
- (id)initWithName:(NSString *)name
{
self = [super initWithName:name];
if (self)
{
self.eating = 5; //初始化食量
self.birthCycle = 4; //生产周期
self.age = 0;
}
return self;
}
////--------生产方法的重写(子)---------
- (Animal *)birth:(NSString *)code {
NSString *name = [NSString stringWithFormat:@"袋鼠%@",code];
Kangaroo *kangaroo = [[[Kangaroo alloc] initWithName:name] autorelease];
NSLog(@"袋鼠生产了一个小袋鼠,名字:%@",name);
return kangaroo;
}
标签:style io ar for sp strong on log cti
原文地址:http://blog.csdn.net/pengyuan_d/article/details/40553713