A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© why19910522 中级黑马   /  2015-8-14 23:33  /  542 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. #import <Foundation/Foundation.h>
  2. //定义狗类
  3. @interface Dog : NSObject
  4. {
  5.     @public;
  6.     NSString *_color;//颜色
  7.     int _speed;//速度
  8.     NSString *_sex;//性别
  9.     float _weight;//体重
  10. }
  11. -(void)eat:(float *)weight;//吃饭
  12. -(void)bark:(NSString *)color and:(int)speed and:(NSString *)sex and:(float) weight;//叫
  13. -(void)run:(int)speed and:(float *)weight;//跑步
  14. +(int)compareColor:(NSString *)dogColorOne and :(NSString *)dogColorTwo;//比较两个狗的颜色
  15. +(int)compareSpeed:(int)dogSpeedOne and :(int)dogSpeedTwo;//比较两个狗的速度
  16. @end

  17. @implementation Dog
  18. //狗每吃一次体重就增加0.5kg,输出吃完后的体重
  19. -(void)eat:(float *)weight{
  20.     *weight += 0.5f;
  21.     NSLog(@"吃完饭后体重为:%.1fkg",*weight);
  22. }
  23. //狗叫,输出所有狗的属性
  24. -(void)bark:(NSString *)color and:(int)speed and:(NSString *)sex and:(float) weight{
  25.     NSLog(@"狗的颜色是:%@,速度是:%dm/s,性别是:%@,体重是:%.1fkg",color,speed,sex,weight);
  26. }
  27. //狗每跑步一次体重就减少0.5kg,输出跑步速度及跑步后的体重
  28. -(void)run:(int)speed and:(float *)weight{
  29.     *weight -= 0.5f;
  30.     NSLog(@"吃完饭后体重为:%.1fkg,速度为:%dm/s",*weight,speed);
  31. }
  32. //比较两个狗的颜色,颜色一样返回YES,不一样返回NO
  33. +(int)compareColor:(NSString *)dogColorOne and :(NSString *)dogColorTwo{
  34.     if (0 == (((int)dogColorOne) - ((int)dogColorTwo))) {
  35.         NSLog(@"两个狗的颜色一样");
  36.         return YES;
  37.     }
  38.         NSLog(@"两个狗的颜色不一样");
  39.         return NO;
  40. }
  41. //比较两个狗的速度,返回速度差
  42. +(int)compareSpeed:(int)dogSpeedOne and :(int)dogSpeedTwo{
  43.     if (dogSpeedOne > dogSpeedTwo) {
  44.         return dogSpeedOne-dogSpeedTwo;
  45.     }
  46.     return dogSpeedTwo-dogSpeedOne;
  47. }
  48. @end

  49. int main(){
  50.     @autoreleasepool {
  51. //        创建两个狗对象
  52.         Dog *dogOne = [Dog new];
  53.         Dog *dogTwo = [Dog new];
  54. //        分别给两个狗的属性赋值
  55.         dogOne->_color = @"白色";
  56.         dogTwo->_color = @"黑色";
  57.         dogOne->_sex = @"公狗";
  58.         dogTwo->_sex = @"母狗";
  59.         dogOne->_speed = 20;
  60.         dogTwo->_speed = 18;
  61.         dogOne->_weight = 35.0f;
  62.         dogTwo->_weight = 33.3f;
  63. //        狗叫
  64.         [dogOne bark:dogOne->_color and:dogOne->_speed and:dogOne->_sex and:dogOne->_weight];
  65. //        狗吃一顿饭
  66.         [dogOne eat:&(dogOne->_weight)];
  67. //        狗跑步
  68.         [dogOne run:dogOne->_speed and:&(dogOne->_weight)];
  69. //        比较两个狗的颜色
  70.         int a = [Dog compareColor:dogOne->_color and:dogTwo->_color];
  71.         NSLog(@"%d",a);
  72. //        比较两个狗的速度
  73.         int difSpeed = [Dog compareSpeed:dogOne->_speed and:dogTwo->_speed];
  74.         NSLog(@"两个狗的速度差为:%dm/s",difSpeed);
  75.     }
  76.     return 0;
  77. }
复制代码


Snip20150814_4.png (117.8 KB, 下载次数: 6)

运行结果

运行结果

2 个回复

倒序浏览
感觉好高大上啊!不会不会的
回复 使用道具 举报
唯爱丶 发表于 2015-8-14 23:48
感觉好高大上啊!不会不会的

别着急,等学到那个地方了就能明白了,不过我这个写的不好,还需要改进
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马