两个类:Person 和 Dog;
person:属性:名字 ,狗;
方法:遛狗;
Dog:属性:狗名,颜色;
方法:跑,捡球,叫;
(自定义初始化)
人:
9点:带狗出去跑,
10点:和狗玩捡球游戏;
11点:逗狗叫;
(可以传入时间)
===============
Dog.h
{
NSString * _name;
NSString * _color;
}
- (id)initWithName:(NSString *)newName WithColor:(NSString *)newColor;
- (void)run;
- (void)playBall;
- (void)call;
Dog.m
- (id)initWithName:(NSString *)newName WithColor:(NSString *)newColor{
[super init];
if(self){
_name = newName;
_color = newColor;
}
return self;
}
- (void)run{
NSLog(@"颜色为%@的%@在跑",_color,_name);
}
- (void)playBall{
NSLog(@"颜色为%@的%@在玩球",_color,_name);
}
- (void)call{
NSLog(@"颜色为%@的%@在叫",_color,_name);
}
Person.h
#import "Dog.h"
{
NSString * _name;
Dog * _dog;
}
- (void)setName:(NSString *)newName;
- (void)setDog:(Dog *)newDog;
- (void)playDog:(NSInteger)time;
Person.m
- (void)setName:(NSString *)newName{
_name = newName;
}
- (void)setDog:(Dog *)newDog;
- (void)playDog:(NSInteger)time;
- (void)setName:(NSString *)newName{
_name = newName;
}
- (void)setDog:(Dog *)newDog{
_dog = newDog;
}
- (void)playDog:(NSInteger)time{
if(time == 9){
[_dog run];
}else if(time == 10){
[_dog playBall];
}
else if(time == 10){
[_dog call]
}
else{
NSLog(@"休息中...");
}
}
main.m
Dog * dog = [[Dog alloc]initWithName:@"小黑" WithColor:@"黑色"];
Person * p1 = [[Person alloc]init];
p1.newName = @"小张";
p1.newDog = dog;
int time;
do{
NSLog@"请输入时间:";
scanf("%i",&time);
p1.teme=time;
}while(time>=9&&time<=11);