- #import <Foundation/Foundation.h>
- //定义狗类
- @interface Dog : NSObject
- {
- @public;
- NSString *_color;//颜色
- int _speed;//速度
- NSString *_sex;//性别
- float _weight;//体重
- }
- -(void)eat:(float *)weight;//吃饭
- -(void)bark:(NSString *)color and:(int)speed and:(NSString *)sex and:(float) weight;//叫
- -(void)run:(int)speed and:(float *)weight;//跑步
- +(int)compareColor:(NSString *)dogColorOne and :(NSString *)dogColorTwo;//比较两个狗的颜色
- +(int)compareSpeed:(int)dogSpeedOne and :(int)dogSpeedTwo;//比较两个狗的速度
- @end
- @implementation Dog
- //狗每吃一次体重就增加0.5kg,输出吃完后的体重
- -(void)eat:(float *)weight{
- *weight += 0.5f;
- NSLog(@"吃完饭后体重为:%.1fkg",*weight);
- }
- //狗叫,输出所有狗的属性
- -(void)bark:(NSString *)color and:(int)speed and:(NSString *)sex and:(float) weight{
- NSLog(@"狗的颜色是:%@,速度是:%dm/s,性别是:%@,体重是:%.1fkg",color,speed,sex,weight);
- }
- //狗每跑步一次体重就减少0.5kg,输出跑步速度及跑步后的体重
- -(void)run:(int)speed and:(float *)weight{
- *weight -= 0.5f;
- NSLog(@"吃完饭后体重为:%.1fkg,速度为:%dm/s",*weight,speed);
- }
- //比较两个狗的颜色,颜色一样返回YES,不一样返回NO
- +(int)compareColor:(NSString *)dogColorOne and :(NSString *)dogColorTwo{
- if (0 == (((int)dogColorOne) - ((int)dogColorTwo))) {
- NSLog(@"两个狗的颜色一样");
- return YES;
- }
- NSLog(@"两个狗的颜色不一样");
- return NO;
- }
- //比较两个狗的速度,返回速度差
- +(int)compareSpeed:(int)dogSpeedOne and :(int)dogSpeedTwo{
- if (dogSpeedOne > dogSpeedTwo) {
- return dogSpeedOne-dogSpeedTwo;
- }
- return dogSpeedTwo-dogSpeedOne;
- }
- @end
- int main(){
- @autoreleasepool {
- // 创建两个狗对象
- Dog *dogOne = [Dog new];
- Dog *dogTwo = [Dog new];
- // 分别给两个狗的属性赋值
- dogOne->_color = @"白色";
- dogTwo->_color = @"黑色";
- dogOne->_sex = @"公狗";
- dogTwo->_sex = @"母狗";
- dogOne->_speed = 20;
- dogTwo->_speed = 18;
- dogOne->_weight = 35.0f;
- dogTwo->_weight = 33.3f;
- // 狗叫
- [dogOne bark:dogOne->_color and:dogOne->_speed and:dogOne->_sex and:dogOne->_weight];
- // 狗吃一顿饭
- [dogOne eat:&(dogOne->_weight)];
- // 狗跑步
- [dogOne run:dogOne->_speed and:&(dogOne->_weight)];
- // 比较两个狗的颜色
- int a = [Dog compareColor:dogOne->_color and:dogTwo->_color];
- NSLog(@"%d",a);
- // 比较两个狗的速度
- int difSpeed = [Dog compareSpeed:dogOne->_speed and:dogTwo->_speed];
- NSLog(@"两个狗的速度差为:%dm/s",difSpeed);
- }
- return 0;
- }
复制代码
|
|