/*
2.结合前面的“狗”类,设计一个“人”类
1> 属性
* 姓名
* 狗(养了一条狗)
2> 行为
* 喂狗:每喂一次,狗就会执行“吃”这个行为
* 遛狗:每溜一次,狗就会执行“跑”这个行为
*/
/*
设计一个”狗“类
1> 属性
* 颜色
* 速度(单位是m/s)
* 性别
* 体重(单位是kg)
2> 行为
* 吃:每吃一次,体重增加0.5kg,输出吃完后的体重
* 吠(叫):输出所有的属性
* 跑:每吃一次,体重减少0.5kg,输出速度和跑完后的体重
* 比较颜色:跟别的狗比较颜色,如果一样,两个值做减法得零,返回NO(零值),不一样,做减法得到非零值,返回YES(1)
*/
#import<Foundation/Foundation.h>
typedef enum color{kColorBlack,kColorWhite,kColorYellow} iColor;
typedef enum sex{kSexGong,kSexMu,kSexYao} iSex;
/**
* Dog
* 属性 颜色 速度 性别 体重
*/
@interface Dog : NSObject
{
@public
iColor _color;
int _speed;
iSex _sex;
float _weight;
}
-(void)eat;
-(void)run;
-(void)wake;
-(void)comper:(Dog *)dogcolor;
-(int)Cspeed:(Dog *)dogspeed;
@end
@implementation Dog
-(void)eat
{
_weight=_weight+0.5;
NSLog(@"dog体重增加了%.2fkg",_weight);
}
-(void)run
{
_weight=_weight-0.5;
NSLog(@"dog体重减少了%.2fkg",_weight);
}
-(void)wake
{
NSLog(@"dog的颜色是%d速度是%d性别是%d体重是%.2f",_color,_speed,_sex,_weight);
}
-(void)comper:(Dog *)dogcolor
{
if(_color==dogcolor->_color)
{
NSLog(@"颜色一样");
}
else
{
NSLog(@"颜色不一样");
}
}
-(int)Cspeed:(Dog *)dogspeed
{
if(_speed>dogspeed->_speed)
{
return _speed-dogspeed->_speed;
}
else
{
return dogspeed->_speed-_speed;
}
}
@end
@interface Person : NSObject
{
@public
NSString *_name;
Dog *_dog;
}
-(void)fooddog:(Dog *)dog;
-(void)liudog:(Dog *)dog;
@end
@implementation Person
-(void)fooddog:(Dog *)dog
{
[dog eat];
NSLog(@"我在吃了");
}
-(void)liudog:(Dog *)dog
{
[dog run];
NSLog(@"我在刘圈");
}
@end
int main()
{
Dog *dog=[Dog new];
dog->_color=0;
dog->_speed=10;
dog->_sex=1;
dog->_weight=20.1;
Dog *dog1=[Dog new];
dog1->_color=1;
dog1->_speed=18;
[dog eat];
[dog run];
[dog wake];
[dog comper:dog1];
int a=[dog Cspeed:dog1];
NSLog(@"速度差是%d",a);
Person *_wangcai=[Person new];
_wangcai->_name=@"李白";
[_wangcai fooddog:dog1];
[_wangcai liudog:dog1];
}
/*
2.结合前面的“狗”类,设计一个“人”类
1> 属性
* 姓名
* 狗(养了一条狗)
2> 行为
* 喂狗:每喂一次,狗就会执行“吃”这个行为
* 遛狗:每溜一次,狗就会执行“跑”这个行为
*/
/*
设计一个”狗“类
1> 属性
* 颜色
* 速度(单位是m/s)
* 性别
* 体重(单位是kg)
2> 行为
* 吃:每吃一次,体重增加0.5kg,输出吃完后的体重
* 吠(叫):输出所有的属性
* 跑:每吃一次,体重减少0.5kg,输出速度和跑完后的体重
* 比较颜色:跟别的狗比较颜色,如果一样,两个值做减法得零,返回NO(零值),不一样,做减法得到非零值,返回YES(1)
*/
#import<Foundation/Foundation.h>
typedef enum color{kColorBlack,kColorWhite,kColorYellow} iColor;
typedef enum sex{kSexGong,kSexMu,kSexYao} iSex;
/**
* Dog
* 属性 颜色 速度 性别 体重
*/
@interface Dog : NSObject
{
@public
iColor _color;
int _speed;
iSex _sex;
float _weight;
}
-(void)eat;
-(void)run;
-(void)wake;
-(void)comper:(Dog *)dogcolor;
-(int)Cspeed:(Dog *)dogspeed;
@end
@implementation Dog
-(void)eat
{
_weight=_weight+0.5;
NSLog(@"dog体重增加了%.2fkg",_weight);
}
-(void)run
{
_weight=_weight-0.5;
NSLog(@"dog体重减少了%.2fkg",_weight);
}
-(void)wake
{
NSLog(@"dog的颜色是%d速度是%d性别是%d体重是%.2f",_color,_speed,_sex,_weight);
}
-(void)comper:(Dog *)dogcolor
{
if(_color==dogcolor->_color)
{
NSLog(@"颜色一样");
}
else
{
NSLog(@"颜色不一样");
}
}
-(int)Cspeed:(Dog *)dogspeed
{
if(_speed>dogspeed->_speed)
{
return _speed-dogspeed->_speed;
}
else
{
return dogspeed->_speed-_speed;
}
}
@end
@interface Person : NSObject
{
@public
NSString *_name;
Dog *_dog;
}
-(void)fooddog:(Dog *)dog;
-(void)liudog:(Dog *)dog;
@end
@implementation Person
-(void)fooddog:(Dog *)dog
{
[dog eat];
NSLog(@"我在吃了");
}
-(void)liudog:(Dog *)dog
{
[dog run];
NSLog(@"我在刘圈");
}
@end
int main()
{
Dog *dog=[Dog new];
dog->_color=0;
dog->_speed=10;
dog->_sex=1;
dog->_weight=20.1;
Dog *dog1=[Dog new];
dog1->_color=1;
dog1->_speed=18;
[dog eat];
[dog run];
[dog wake];
[dog comper:dog1];
int a=[dog Cspeed:dog1];
NSLog(@"速度差是%d",a);
Person *_wangcai=[Person new];
_wangcai->_name=@"李白";
[_wangcai fooddog:dog1];
[_wangcai liudog:dog1];
}
|
|