------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
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 }
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 }
黑马程序员---Objective-C基础学习---一道课后习题引发的思考
原文:http://www.cnblogs.com/zss-itcast/p/soso4431.html