黑马程序员---Objective-C基础学习---一道课后习题引发的思考

------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

一道课后习题引发的思考
/*
需求:设计一个类Point2D,用来表示二维平面中某个点
1> 属性
* double x
* double y
 
2> 方法
* 属性相应的set和get方法
* 设计一个对象方法同时设置x和y
* 设计一个对象方法计算跟其他点的距离
* 设计一个类方法计算两个点之间的距离
 
3> 提示
* C语言的math.h中有个函数:double pow(double n,double m); 计算n的m次方
* C语言的math.h中有个函数:double sqrt(double n); 计算根号n的值(对n进行开根)
*/
/*
一、下面是我的代码:
 1 #import <Foundation/Foundation.h>
 2 #import <math.h>
 3  
 4 @interface Point2D : NSObject
 5  
 6 //
 7 {
 8     double _x; // x的值
 9     double _y; // y的值
10 }
11  
12 // x的get和set方法
13  
14 - (void)setX : (double)x;
15 - (double)x;
16  
17 // y的get和set方法
18 - (void)setY : (double)y;
19 - (double)y;
20  
21 // 同时设置x和y的值
22 - (void)setX : (double)x andY : (double)y;
23  
24 // 计算和其他点得距离
25 - (double)distanceWithOtherPoint : (Point2D *)p;
26  
27 // 计算点和点之间的距离
28 + (double)distanceBetweenPoint1 : (Point2D *)p1 andPoint2 : (Point2D *)p2;
29  
30 @end // Point2D声明
31  
32 // 方法
33 @implementation Point2D
34  
35 - (void)setX : (double)x
36 {
37     _x = x;
38 }
39 - (double)x
40 {
41     return _x;
42 }
43 - (void)setY : (double)y
44 {
45     _y = y;
46 }
47 - (double)y
48 {
49     return _y;
50 }
51 - (void)setX : (double)x andY : (double)y
52 {
53     _x = x;
54     _y = y;
55    
56      // [self setX:x]; //设置x过程中可能涉及到过滤的问题,如果x的值不符合要求,我们需要处理
57     // [self setY:y];
58      
59 }
60 - (double)distanceWithOtherPoint : (Point2D *)p
61 {   
62     double x1 = [p x];
63     double y1 = [p y];
64     return  sqrt(pow(_x-x1, 2) + pow(_y-y1, 2));
65 }
66 + (double)distanceBetweenPoint1 : (Point2D *)p1 andPoint2 : (Point2D *)p2
67 {
68     double x1 = [p1 x];
69     double y1 = [p1 y];
70     double x2 = [p2 x];
71     double y2 = [p2 x];
72     return sqrt(pow(x1-x2, 2) + pow(y1-y2, 2));
73 }
74  
75 @end //Point2D
76  
77 //主函数
78 int main()
79 {
80     Point2D *pt1 = [Point2D new];
81     Point2D *pt2 = [Point2D new];
82     Point2D *pt3 = [Point2D new];
83     //[pt1 setX : 2 andY : 2];
84     [pt2 setX : 4 andY : 4];   
85     [pt3 setX : 9 andY : 9];
86     double distance1 = [pt1distanceWithOtherPoint : pt2];
87     double distance2 = [Point2DdistanceBetweenPoint1 : pt2 andPoint2 : pt3];
88     NSLog(@"The distande with other point is %f",distance1);
89     NSLog(@"The distande between point1 and point2 is %f",distance2);
90    
91     return 0;
92    
93 }

 

 
二、引发的思考
第一、如何写好注
我对注释一直都不是很重视,读大学写代码也很少写注释,也不知道怎么写,什么时候该写,什么时候不该写,没什么概念,通过这个作业题正好可以练习如何写好注释,写注释主要就是为了可读性,多看优秀代码,看老师是如何注释的,养成写注释习惯。
第二、避免重复代
//  计算和其他点得距离 我的方法一
1 - (double)distanceWithOtherPoint : (Point2D *)p
2 {   
3     double x1 = [p x];
4     double y1 = [p y];
5     return  sqrt(pow(_x-x1, 2) + pow(_y-y1,2));
6 }

 

//  方法二,因为后面有一个求两点之间距离的方法,两者有相似之处,需要避免重复代码
1 - (double)distanceWithOther:(Point2D *)other
2 {
3     // 不要再傻乎乎算一遍了,直接调用类方法即可
4     return [Point2DdistanceBetweenPoint1:self andPoint2:other];
5 }

 

找代码之间的共同点,写好一个方法,其他方法可以间接调用
 
第三、并不是代 越精 越好,有的时候为了可读性,代码需要写的详细一点
//  求两点之间距离  ,我的  方法一
1 + (double)distanceBetweenPoint1 : (Point2D *)p1 andPoint2 : (Point2D *)p2
2 {
3     double x1 = [p1 x];
4     double y1 = [p1 y];
5     double x2 = [p2 x];
6     double y2 = [p2 x];
7     returnsqrt(pow(x1-x2, 2) + pow(y1-y2, 2));  // 一行代码解决,但可读性不强
8 //
9 }

 

//   第二种方法  分步骤,思路清晰
 1 + (double)distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2
 2 {
 3     // 两点距离公式:( (x1-x2)的平方 + (y1-y2)的平方 )开根
 4     // x1-x2
 5     double xDelta = [p1 x] - [p2x];
 6     // (x1-x2)的平方
 7     double xDeltaPingFang =pow(xDelta, 2);
 8     // y1-y2
 9     double yDelta = [p1 y] - [p2y];
10     // (y1-y2)的平方
11     double yDeltaPingFang =pow(yDelta, 2);
12     return sqrt(xDeltaPingFang + yDeltaPingFang);
13 }
 
第四、 经验问题
// 同时设置x的值和y的值 ,方法一
 1 - (void)setX : (double)x andY : (double)y
 2 {
 3     _x = x;
 4     _y = y;
 5 }  // 这里没考虑到设置x过程中可能涉及到过滤的问题,如果x的值不符合要求,我们需要处理
 6  
 7 // 方法二
 8 - (void)setX : (double)x andY : (double)y
 9 {
10     [self setX:x];  // 这里调用set方法设置x和y的值,考虑了x过滤问题
11     [self setY:y];
12 }

 

这是我做这道练习题的一些思考,欢迎批评指正!

转载于:https://www.cnblogs.com/zss-itcast/p/soso4431.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值