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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Harvey_ios 中级黑马   /  2015-5-9 17:07  /  997 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

之前我发了一个帖子一道作业题引发的思考,今天的作业题是在上次题目基础上实现的,题目如下:
  1. /**
  2. 6.设计一个类Circle,用来表示二维平面中的圆
  3. 1> 属性
  4. * double radius (半径)
  5. * Point2D *point (圆心)

  6. 2> 方法
  7. * 属性相应的set和get方法
  8. * 设计一个对象方法判断跟其他圆是否相交(重叠返回YES,否则返回NO)
  9. * 设计一个类方法判断两个圆是否相交(重叠返回YES,否则返回NO)
  10. */
复制代码

我的思路是:设计一个Circle类继承Point2D,因为圆心也是一个点,Circle类通过继承可以使用父类中的实例变量和方法。
我的代码如下:
  1. #import <Foundation/Foundation.h>
  2. #import <math.h>

  3. // 点
  4. @interface Point2D : NSObject
  5. {
  6.     double _x; // x值
  7.     double _y; // y值
  8.    
  9. }
  10. // x值的getter和setter
  11. - (void)setX:(double)x;
  12. - (double)x;

  13. // y值的getter和setter
  14. - (void)setY:(double)y;
  15. - (double)y;

  16. // 同时设置x和y
  17. - (void)setX:(double)x andY:(double)y;

  18. // 计算跟其他点的距离
  19. - (double)distanceWithOther:(Point2D *)other;

  20. // 计算两个点之间的距离
  21. + (double)distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2;



  22. @end

  23. @implementation Point2D
  24. // x值的getter和setter
  25. - (void)setX:(double)x
  26. {
  27.     _x = x;
  28. }
  29. - (double)x
  30. {
  31.     return _x;
  32. }

  33. // y值的getter和setter
  34. - (void)setY:(double)y
  35. {
  36.     _y = y;
  37. }
  38. - (double)y
  39. {
  40.     return _y;
  41. }
  42. // 同时设置x和y
  43. - (void)setX:(double)x andY:(double)y
  44. {
  45.     // 第1种思路
  46.     // _x = x;
  47.     // _y = y;
  48.    
  49.     // 第2种思路
  50.         [self setX:x];
  51.         [self setY:y];
  52. }

  53. // 计算跟其他点的距离
  54. - (double)distanceWithOther:(Point2D *)other
  55. {
  56.     // 不要再傻乎乎算一遍了,直接调用类方法即可
  57.     return [Point2D distanceBetweenPoint1:self andPoint2:other];
  58. }

  59. // 计算两个点之间的距离
  60. + (double)distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2
  61. {
  62.     // 两点距离公式:( (x1-x2)的平方 + (y1-y2)的平方 )开根
  63.    
  64.     // x1-x2
  65.     double xDelta = [p1 x] - [p2 x];
  66.     // (x1-x2)的平方
  67.     double xDeltaPingFang = pow(xDelta, 2);
  68.    
  69.     // y1-y2
  70.     double yDelta = [p1 y] - [p2 y];
  71.     // (y1-y2)的平方
  72.     double yDeltaPingFang = pow(yDelta, 2);
  73.    
  74.     return sqrt(xDeltaPingFang + yDeltaPingFang);
  75. }
  76. @end
  77. // Circle类  继承Point2D
  78. @interface Circle : Point2D
  79. {
  80.     double _radius; // 半径
  81. }
  82. // radius值的setter和getter方法
  83. - (void)setRadius:(double)radius;
  84. - (double)radius;
复制代码
编译链接发现有3个警告和1个错误:
  1. circle.m:130:15: warning: 'Point2D' may not respond to 'radius'
  2.     double r=[c1 radius] + [c2 radius];
  3.               ^
  4. circle.m:130:29: warning: 'Point2D' may not respond to 'radius'
  5.     double r=[c1 radius] + [c2 radius];
  6.                             ^
  7. circle.m:131:16: warning: class method '+diatanceBetweenPoint1:andPoint2:' not
  8.       found (return type defaults to 'id')
  9.     double dis=[Point2D diatanceBetweenPoint1:c1 andPoint2: c2];
  10.                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  11. circle.m:131:12: error: initializing 'double' with an expression of incompatible
  12.       type 'id'
  13.     double dis=[Point2D diatanceBetweenPoint1:c1 andPoint2: c2];
  14.            ^   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  15. 3 warnings and 1 error generated.
  16. zous-Mac:objective-c shao$
复制代码
记得老师说过父类不能调用子类独有的方法,这导致了前两个警告,但是后面的警告和错误是怎么回事呢?



2 个回复

倒序浏览
  1. //上面代码不完整,这里是我的完整代码
复制代码


回复 使用道具 举报
  1. /**
  2. 6.设计一个类Circle,用来表示二维平面中的圆
  3. 1> 属性
  4. * double radius (半径)
  5. * Point2D *point (圆心)

  6. 2> 方法
  7. * 属性相应的set和get方法
  8. * 设计一个对象方法判断跟其他圆是否相交(重叠返回YES,否则返回NO)
  9. * 设计一个类方法判断两个圆是否相交(重叠返回YES,否则返回NO)
  10. */

  11. #import <Foundation/Foundation.h>
  12. #import <math.h>

  13. // 点
  14. @interface Point2D : NSObject
  15. {
  16.     double _x; // x值
  17.     double _y; // y值
  18.    
  19. }
  20. // x值的getter和setter
  21. - (void)setX:(double)x;
  22. - (double)x;

  23. // y值的getter和setter
  24. - (void)setY:(double)y;
  25. - (double)y;

  26. // 同时设置x和y
  27. - (void)setX:(double)x andY:(double)y;

  28. // 计算跟其他点的距离
  29. - (double)distanceWithOther:(Point2D *)other;

  30. // 计算两个点之间的距离
  31. + (double)distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2;



  32. @end

  33. @implementation Point2D
  34. // x值的getter和setter
  35. - (void)setX:(double)x
  36. {
  37.     _x = x;
  38. }
  39. - (double)x
  40. {
  41.     return _x;
  42. }

  43. // y值的getter和setter
  44. - (void)setY:(double)y
  45. {
  46.     _y = y;
  47. }
  48. - (double)y
  49. {
  50.     return _y;
  51. }
  52. // 同时设置x和y
  53. - (void)setX:(double)x andY:(double)y
  54. {
  55.     // 第1种思路
  56.     // _x = x;
  57.     // _y = y;
  58.    
  59.     // 第2种思路
  60.         [self setX:x];
  61.         [self setY:y];
  62. }

  63. // 计算跟其他点的距离
  64. - (double)distanceWithOther:(Point2D *)other
  65. {
  66.     // 不要再傻乎乎算一遍了,直接调用类方法即可
  67.     return [Point2D distanceBetweenPoint1:self andPoint2:other];
  68. }

  69. // 计算两个点之间的距离
  70. + (double)distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2
  71. {
  72.     // 两点距离公式:( (x1-x2)的平方 + (y1-y2)的平方 )开根
  73.    
  74.     // x1-x2
  75.     double xDelta = [p1 x] - [p2 x];
  76.     // (x1-x2)的平方
  77.     double xDeltaPingFang = pow(xDelta, 2);
  78.    
  79.     // y1-y2
  80.     double yDelta = [p1 y] - [p2 y];
  81.     // (y1-y2)的平方
  82.     double yDeltaPingFang = pow(yDelta, 2);
  83.    
  84.     return sqrt(xDeltaPingFang + yDeltaPingFang);
  85. }
  86. @end

  87. @interface Circle : Point2D
  88. {
  89.     double _radius;
  90. }
  91. // radius值的setter和getter方法
  92. - (void)setRadius:(double)radius;
  93. - (double)radius;
  94. - (bool)xjWithOtherCircle :(Circle *)other;
  95. + (bool)xjBetweenCircle1: (Circle *)c1 andCircle2:(Circle *)c2;
  96. @end
  97. @implementation Circle
  98. - (void)setRadius:(double)radius
  99. {
  100.     _radius = radius;
  101. }
  102. - (double)radius
  103. {
  104.     return _radius;
  105. }
  106. - (bool)xjWithOtherCircle :(Circle *)other
  107. {
  108.     return [Circle xjBetweenCircle1:self andCircle2:other];
  109. }
  110. + (bool)xjBetweenCircle1: (Point2D *)c1 andCircle2:(Point2D *)c2
  111. {
  112.     // 判断两圆是否相交:两圆心的距离和半径之和比较大小
  113.     //1、如果圆心距离大于半径之和,则不想交
  114.     //2、如果圆心半径小于或等于半径之和,则相交
  115.     double r=[c1 radius] + [c2 radius];
  116.    double dis=[Point2D diatanceBetweenPoint1:c1 andPoint2: c2];
  117.     if(r<dis)
  118.         return NO;
  119.     else
  120.         return YES;
  121.    
  122. }
  123. @end
  124. int main()
  125. {
  126.     Circle *p1 = [Circle new];
  127.     [p1 setX:10 andY:10];
  128.     [p1 setRadius:3];
  129.    
  130.    Circle *p2 = [Circle new];
  131.     [p2 setX:13 andY:14];
  132.     [p2 setRadius:1];
  133.    
  134.    
  135.    // double d1 = [p1 distanceWithOther:p2];
  136.    //
  137.     double d2 = [Point2D distanceBetweenPoint1:p1 andPoint2:p2];
  138.    
  139.     NSLog(@"%d", [Circle xjBetweenCircle1:p1 andCircle2:p2]);
  140.    
  141.     return 0;
  142. }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马