http://bbs.itheima.com/thread-261940-1-1.html
/*
2.结合前面的“狗”类,设计一个“人”类
1> 属性
* 姓名
* 狗(养了一条狗)
2> 行为
* 喂狗:每喂一次,狗就会执行“吃”这个行为
* 遛狗:每溜一次,狗就会执行“跑”这个行为
*/
#import <Foundation/Foundation.h>
@interface Dog : NSObject
{
@public
NSString *_name;
int _color;
float _speed;
char _sex;
float _weight;
}
-(void)eat;
-(void)fate;
-(void)run;
-(NSString *)cmpColor:(Dog *) otherDog;
-(int)cmpSpeed:(Dog *) otherDog;
@end
@interface Person : NSObject
{
@public
NSString *_name;
Dog * _dag;
}
-(void) feedDog: (Dog *) dag;
-(void) walkWithDog: (Dog *) dag;
@end
@implementation Dog
-(void)eat{
_weight+=0.5;
NSLog(@"%@,体重:%.2lf",_name,_weight);
}
-(void)fate{
NSLog(@"%@,颜色:%d,性别%c,体重:%.2lf,速度:%.2lf\n",_name,_color,_sex,_weight,_speed);
}
-(void)run{
_weight-=0.5;
NSLog(@"%@,体重:%.2lf",_name,_weight);
}
-(NSString *)cmpColor:(Dog *) otherDog {
if (_color == otherDog ->_color) {
return @"YES";
} else {
return @"NO";
}
}
-(int)cmpSpeed:(Dog *) otherDog {
return _speed - otherDog->_speed;
}
@end
@implementation Person
-(void) feedDog: (Dog *) dag {
[dag eat];
}
-(void) walkWithDog: (Dog *) dag {
[dag run];
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
Dog * yellow = [Dog new];
yellow->_name= @"大黄";
yellow->_color= 1;
yellow->_sex= 'm';
yellow->_weight= 125;
yellow->_speed= 12;
Dog * black = [Dog new];
black->_name= @"小黑";
black->_color= 2;
black->_sex= 'f';
black->_weight= 75;
black->_speed= 10;
Person * darin = [Person new];
darin -> _name = @"darin";
darin -> _dag = yellow;
[darin feedDog: yellow];
[darin walkWithDog: black];
NSLog(@"The, end!");
}
return 0;
}
|
|