要求:
* 类名、属性名、属性类型、方法名、方法参数、方法返回值自拟
* 自己写main函数测试设计是否合理
1.设计一个”狗“类
1> 属性
* 颜色
* 速度(单位是m/s)
* 性别
* 体重(单位是kg)
2> 行为
* 吃:每吃一次,体重增加0.5kg,输出吃完后的体重
* 吠(叫):输出所有的属性
* 跑:每吃一次,体重减少0.5kg,输出速度和跑完后的体重
* 比较颜色:跟别的狗比较颜色,如果一样,返回YES,不一样,返回NO
* 比较速度:跟别的狗比较速度,返回速度差(自己的速度 - 其他狗的速度)
2.结合前面的“狗”类,设计一个“人”类
1> 属性
* 姓名
* 狗(养了一条狗)
2> 行为
* 喂狗:每喂一次,狗就会执行“吃”这个行为
* 遛狗:每溜一次,狗就会执行“跑”这个行为
# include <Foundation/Foundation.h>
//颜色
typedef enum
{
colorBlack;
colorWhite;
colorYellow;
}Color;
//性别
typedef enum
{
sexMan;
sexWoman;
sexUnknown;
}Sex;
//定义狗类
@interface Dog : NSobject
{
Color color;//颜色
int speed;//速度
Sex sex;//性别
double weight;//体重
}
- (void) eat;
- (void) bank;
- (void) run;
- (bool) compareColorWithOthers:(Dog*) other;
- (int) compareSpeedWithOthers:(Dog*) other;
@end
//定义狗方法
@implementation Dog
- (void) eat
{
weight +=0.5;
NSLog(@"吃:每吃一次,体重增加:%d",weight);
}
- (void) bank;
{
NSLog(@"狗的颜色是:%d,速度是:%d km/h,性别是:%d,体重是:%f公斤 ",color,speed,sex,weight);
}
- (void) run
{
speed -=0.5;
NSLog(@"狗的速度:%dkm/h,狗的体重:%f公斤 ",speed,weight);
}
- (bool) compareColorWithOthers:(Dog*) other
{
return color ==other->color;
}
- (bool) compareSpeedWithOthers:(Dog*) other
{
return spee ==other->speed;
}
end
//人类
@interface People :NSobject
{
@public
char name;
Dog *dog;
}
- (void) feeddog;
- (void) walkdog;
@end;
//人的方法
@implementation People
{
- (void) feeddog
{
[dog eat];
}
-(void) walkdog
{
[dog run];
}
}
@end;
至此一个类初步完成