年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 3016|回复: 0

对象操作的一个经典例子

[复制链接]
  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

    发表于 2014-3-14 14:44:11 | 显示全部楼层 |阅读模式
    说明都写在代码注释里了:
    1. /*
    2. 1、设计一个类Point2D,用来表示二维平面中的某个点;
    3.     属性:
    4.         > double _x;
    5.         > double _y;
    6.     方法:
    7.         > 属性相就的set和get方法
    8.         > 设计一个对象方法同时设置x和y的值
    9.         > 设计一个对象方法计算跟其他点的距离
    10.         > 设计一个类方法计算两个点之间的距离
    11.     参考:
    12.         > c语言中的math.h中有个函数:double pow(double n, double m); 计算n的m次方;
    13.         > c语言中的math.h中有个函数:double sqrt(double n); 对n进行开根
    14. 2、设计一个类Circle,用来表示二维平面中的圆
    15.     属性:
    16.         > double _radius (半径)
    17.         > double *_point (圆心)
    18.     方法:
    19.         > 属性相应的set和get方法
    20.         > 设计一个对象方法判断跟其他圆是否会发生重叠现象(重叠返回YES,否则返回NO)
    21.         > 设计一个类方法判断跟其他圆是否会发生重叠现象(重叠返回YES,否则返回NO)
    22. */
    23. #import <Foundation/Foundation.h>
    24. #import <math.h>
    25. @interface Point2D : NSObject {
    26.     double _x;  // x值
    27.     double _y;  // y值
    28. }
    29. // 成员变量_x的set和get方法声明
    30. - (void)setX:(double)x;
    31. - (double)x;
    32. // 成员变量_y的set和get方法声明
    33. - (void)setY:(double)y;
    34. - (double)y;
    35. // 同时设置_x和_y的值方法声明
    36. - (void)setX:(double)x andY:(double)y;
    37. // 类方法计算两点的距离
    38. + (double)distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2;
    39. // 对象方法计算两点的距离
    40. - (double)distanceWithOther:(Point2D *)other;
    41. @end
    42. @implementation Point2D
    43. - (void)setX:(double)x {
    44.     _x = x;
    45. }
    46. - (double)x {
    47.     return _x;
    48. }
    49. - (void)setY:(double)y {
    50.     _y = y;
    51. }
    52. - (double)y {
    53.     return _y;
    54. }
    55. // 同时设置_x和_y的值,可以调用前面给单个变量赋值的set方法
    56. - (void)setX:(double)x andY:(double)y {
    57.     [self setX:x];
    58.     [self setY:y];
    59. }
    60. // 计算两个点的距离:p1(x1, y1)与p2(x2, y2),数学上计算两个点的距离公式为:(((x1-x2)^2) + ((y1 - y2)^2))再开方
    61. + (double)distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2 {
    62.     // p1的x坐标值减去p2的x坐标值
    63.     double xDelta = [p1 x] - [p2 x];
    64.    
    65.     // 调用函数pow()计算xDelta的平方
    66.     double xDeltaPF = pow(xDelta, 2);
    67.    
    68.     // p1的y坐标值减去p2的主坐标值
    69.     double yDelta = [p1 y] - [p2 y];
    70.    
    71.     // 将上述得到的值进行平方运算;
    72.     double yDeltaPF = pow(yDelta, 2);
    73.    
    74.     // 将得到的平方和再调用sqrt函数进行开方运算却可得到两个点的距离
    75.     return sqrt(xDeltaPF + yDeltaPF);
    76. }
    77. - (double)distanceWithOther:(Point2D *)other {
    78.    
    79.     // 为提高代码的运算效率这里也是计算两个点的距离,直接调用类方法计算自身与other点的距离
    80.     return [Point2D distanceBetweenPoint1:self andPoint2:other];
    81.    
    82. }
    83. @end
    84. @interface Circle : NSObject {
    85.     double _radius;  // 半径
    86.    
    87.     Point2D *_point;  // 圆点,组合类,将point类做为圆的一个成员变量
    88. }
    89. // 半径的set和get方法声明
    90. - (void)setRadius:(double)radius;
    91. - (double)radius;
    92. // 圆心的set和get方法
    93. - (void)setPoint:(Point2D *)point;
    94. - (Point2D *)point;
    95. // 两圆是否存在重叠现象的对象方法声明
    96. - (BOOL)isCrossWithOther:(Circle *)other;
    97. // 两圆是否存在重叠现象的类方法声明
    98. + (BOOL)isCrossBetweenCircle1:(Circle *)c1 andCircle2:(Circle *)c2;
    99. @end
    100. @implementation Circle
    101. - (void)setRadius:(double)radius {
    102.     _radius = radius;
    103.    
    104. }
    105. - (double)radius {
    106.     return _radius;
    107. }
    108. - (void)setPoint:(Point2D *)point {
    109.     _point = point;
    110. }
    111. - (Point2D *)point {
    112.     return _point;
    113. }
    114. + (BOOL)isCrossBetweenCircle1:(Circle *)c1 andCircle2:(Circle *)c2 {
    115.    
    116.     // 调用点对象的中计算两点距离的方法计算出两点距离即为圆心距离
    117.     // NSLog(@"两圆心的距离为:%.2f", [Point2D distanceBetweenPoint1:[c1 point] andPoint2:[c2 point]]);
    118.     // 相当于
    119.     // Point2D *p1 = [c1 point];
    120.     // Point2D *p2 = [c2 point];
    121.     // BOOL result = [Point2D distanceBetweenPoint1:p1 andPoint2:p2];
    122.     // return result;
    123.    
    124.     // 使用对象方法实现
    125.     NSLog(@"两圆心的距离为:%.2f", [[c1 point] distanceWithOther:[c2 point]]);
    126.    
    127.     // 将两圆半径打印出来进行对比
    128.     NSLog(@"C1的半径为:%.2f", [c1 radius]);
    129.     NSLog(@"C2的半径为:%.2f", [c2 radius]);
    130.     // 如果两点的距离小于两圆半径之和,说明两圆有重叠
    131.     return [Point2D distanceBetweenPoint1:[c1 point] andPoint2:[c2 point]] < [c2 radius] + [c1 radius];
    132. }
    133. - (BOOL)isCrossWithOther:(Circle *)other {
    134.    
    135.     // 对象方法可以直接调用类方法的实现
    136.     return [Circle isCrossBetweenCircle1:self andCircle2:other];
    137.    
    138. }
    139. @end
    140. int main() {
    141.    
    142.     // 创建一个点对象
    143.     Point2D *p1 = [Point2D new];
    144.     // 点对象进行赋值
    145.     [p1 setX:10 andY:10];
    146.    
    147.     // 创建另外一个点
    148.     Point2D *p2 = [Point2D new];
    149.     [p2 setX:40 andY:50];
    150.    
    151.     // 使用类方法计算两点的距离
    152.     double pointDistance = [Point2D distanceBetweenPoint1:p1 andPoint2:p2];
    153.     // 同样使用对象方法一样可以实现
    154.     // double pointDistance = [p1 distanceWithOther:p2];
    155.    
    156.     // 输出两点的距离值
    157.     // NSLog(@"p1与p2的距离为:%.2f", pointDistance);
    158.    
    159.     // 创建一个圆对象
    160.     Circle *c1 = [Circle new];
    161.     // 基本成员变量进行赋值
    162.     [c1 setPoint:p1];
    163.     [c1 setRadius:30];
    164.    
    165.     // 创建另一个圆对象并对基本成员变量赋值
    166.     Circle *c2 = [Circle new];
    167.     [c2 setPoint:p2];
    168.     [c2 setRadius:30];
    169.    
    170.     // 输出结果,是否有重叠现象
    171.     NSLog(@"%@", [Circle isCrossBetweenCircle1:c1 andCircle2:c2] ? @"两圆有重叠现象" : @"两圆无重叠现象");
    172. /* 相当于以下代码:
    173.     BOOL result = [Circle isCrossBetweenCircle1:c1 andCircle2:c2];
    174.     NSString * str = result ? @"两圆有重叠现象" : @"两圆无重叠现象";
    175.     NSLog(@"%@", str);
    176. */
    177.     // 用对象方法判断
    178.     // NSLog(@"%@", [c1 isCrossWithOther:c2] ? @"两圆有重叠现象" : @"两圆无重叠现象");
    179.     return 0;
    180. }
    复制代码
    输出结果:
    2014-03-14 14:42:07.916 a.out[7268:507] 两圆心的距离为:50.00
    2014-03-14 14:42:07.917 a.out[7268:507] C1的半径为:30.00
    2014-03-14 14:42:07.918 a.out[7268:507] C2的半径为:30.00
    2014-03-14 14:42:07.918 a.out[7268:507] 两圆有重叠现象



    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-5-5 21:23 , Processed in 0.046898 second(s), 21 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表