类方法中可以调用对象方法。
如:
类方法中传入对象参数,在类方法实现中可以调用该对象的对象方法,这样调用这个类方法时造成的影响对象也是该对象。
参考题目:
- /*************************************
- 1. 设计一个类Point2D,用来表示二维平面中某个点
- 1> 属性
- * double x
- * double y
-
- 2> 方法
- * 属性相应的set和get方法
- * 设计一个对象方法同时设置x和y
- * 设计一个对象方法计算跟其他点的距离
- * 设计一个类方法计算两个点之间的距离
-
- 3> 提示
- C语言 math.h里面
- double pow(double n,double m) :计算n的m次方
- double sqrt(double n) :计算根号N的值
- *************************************/
- /*************************************
- 设计一个类Circle,用来表示二维平面中的圆
- 1> 属性
- * double _radius (半径)
- * Point2D *_point (圆心)
- 2> 方法
- * 属性相应的set方法跟get方法
- * 设计一个对象方法判断跟其他圆是否重叠(重叠返回yes,否则返回no)
- * 设计一个类方法判断两个圆是否重叠(重叠返回yes,否则返回No)
- *************************************/
- #import <Foundation/Foundation.h>
- #import <math.h>
- @interface Point2D : NSObject
- {
- double _x;
- double _y;
- }
- - (void)SetPoint2DX : (double)x andY : (double)y;
- - (double)GetPointY;
- - (double)GetPointX;
- - (double)CountMyPointWithOtherPoint : (Point2D *) otherPoint;
- + (double)CountDistancePoint1 : (Point2D *)point1 WithPoint2 : (Point2D *)point2;
- @end
- @interface Circle : NSObject
- {
- double _radius; //半径
- Point2D *_point; //圆心
- }
- - (void)SetRadius : (double)radius ;
- - (void)SetPoint : (Point2D *) point ;
- - (double)GetRadius;
- - (Point2D *)GetPoint;
- - (BOOL)JudgeSelfWithOtherCircle : (Circle *)circle ;
- + (BOOL)JudgeCircle : (Circle *)circle1 WithOtherCircle : (Circle *) circle2 ;
- @end
- @implementation Point2D
- - (void)SetPoint2DX : (double)x andY : (double)y
- {
- _x = x;
- _y = y;
-
- return;
- }
- - (double)GetPointY
- {
- return _y;
- }
- - (double)GetPointX
- {
- return _x;
- }
- - (double)CountMyPointWithOtherPoint : (Point2D *) otherPoint
- {
- return [Point2D CountDistancePoint1 : self WithPoint2 : otherPoint];
-
- }
- + (double)CountDistancePoint1 : (Point2D *)point1 WithPoint2 : (Point2D *)point2
- {
- double x_length,y_length,pTop_length;
-
- //x_length = point1->_x - point2->_x;
- x_length = [point1 GetPointX] - [point2 GetPointX];
- //y_length = point1->_y - point2->_y;
- y_length = [point1 GetPointY] - [point2 GetPointY];
-
- pTop_length = sqrt((pow(x_length,2) + pow(y_length,2)));
-
- //return [point1 CountMyPointWithOtherPoint:point2];
- return pTop_length;
- }
- @end
- @implementation Circle
- - (void)SetRadius : (double)radius
- {
- if(radius > 0)
- self->_radius = radius;
- else
- NSLog(@"SET ERROR!!!");
- return;
- }
- - (void)SetPoint : (Point2D *) point
- {
- _point = point;
- return;
- }
- - (double)GetRadius
- {
- return self->_radius ;
- }
- - (Point2D *)GetPoint
- {
- return self->_point;
- }
- - (BOOL)JudgeSelfWithOtherCircle : (Circle *)circle
- {
-
- return [Circle JudgeCircle : self WithOtherCircle : circle];
- }
- + (BOOL)JudgeCircle : (Circle *)circle1 WithOtherCircle : (Circle *) circle2
- {
- Point2D *p1 = [circle1 GetPoint];
- Point2D *p2 = [circle2 GetPoint];
-
- double length_p1Top2 = [Point2D CountDistancePoint1 : p1 WithPoint2 : p2];
-
- if(length_p1Top2 >= ([circle1 GetRadius] + [circle2 GetRadius]))
- return NO;
- else
- return YES;
-
- return 0;
- }
- @end
- int main()
- {
- Circle *p_Circle1 = [Circle new];
- Circle *p_Circle2 = [Circle new];
-
- [p_Circle1 SetRadius:5];
- [p_Circle2 SetRadius:6];
-
- NSLog(@"圆一的半径为:%.2f",[p_Circle1 GetRadius]);
- NSLog(@"圆二的半径为:%.2f",[p_Circle2 GetRadius]);
-
- Point2D *p_point1 = [Point2D new];
- Point2D *p_point2 = [Point2D new];
-
- [p_point1 SetPoint2DX : 3 andY : 0];
- [p_point2 SetPoint2DX : 3 andY : 4];
- [p_Circle1 SetPoint:p_point1];
- [p_Circle2 SetPoint:p_point2];
-
- NSLog(@"圆一跟圆二重叠的情况是:%d",[Circle JudgeCircle : p_Circle1 WithOtherCircle : p_Circle2]);
-
- // Point2D *p_Point1 ;
- // Point2D *p_Point2 ;
- //
- // p_Point1 = [Point2D new];
- // p_Point2 = [Point2D new];
- //
- // [p_Point1 SetPoint2DX : 0 andY : 0];
- // [p_Point2 SetPoint2DX : 3 andY : 4];
- //
- // NSLog(@"点一到点二的距离为:%.2f",[Point2D CountDistancePoint1 : p_Point1 WithPoint2 : p_Point2]);
-
- return 0;
- }
复制代码 |