设计一个类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进行开根)
一共3个文件
main.m
- #import <Foundation/Foundation.h>
- #import "JwPoint.h"
- int main(int argc, const char * argv[]) {
- @autoreleasepool {
-
- JwPoint * pointA = [JwPoint new];
- JwPoint * pointB = [JwPoint new];
- [pointA setWithX :1 andY :1];
- [pointB setWithX :2 andY :2];
-
- NSLog(@"\nA点与B点的距离为:%.2lf",[pointA getRangeWithPoint :pointB]);
- NSLog(@"\nA点与B点的距离为:%.2lf",[JwPoint getRangeWithPointA :pointA andPointB :pointB]);
-
- }
- return 0;
- }
复制代码
JwPoint.h
- #import <Foundation/Foundation.h>
- @interface JwPoint : NSObject{
- int _x;
- int _y;
- }
- - (void)setX :(int)x;
- - (int)x;
- - (void)setY :(int)y;
- - (int)y;
- - (void)setWithX :(int)X andY:(int)y;
- - (double)getRangeWithPoint :(JwPoint *)point;
- + (double)getRangeWithPointA :(JwPoint *)pointA andPointB :(JwPoint *)pointB;
- @end
复制代码
JwPoint.m
- #import "JwPoint.h"
- #import <Math.h>
- @implementation JwPoint
- - (void)setX :(int)x{
- _x = x;
- }
- - (int)x{
- return _x;
- }
- - (void)setY :(int)y{
- _y = y;
- }
- - (int)y{
- return _y;
- }
- - (void)setWithX :(int)x andY:(int)y{
- _x = x;
- _y = y;
- }
- - (double)getRangeWithPoint :(JwPoint *)point{
- return sqrt((double)((_x - point->_x) * (_x - point->_x) + (_y - point->_y) * (_y - point->_y)));
- }
- + (double)getRangeWithPointA :(JwPoint *)pointA andPointB :(JwPoint *)pointB{
- return sqrt((double)((pointA->_x - pointB->_x) * (pointA->_x - pointB->_x) + (pointA->_y - pointB ->_y) * (pointA->_y - pointB ->_y)));
- }
- @end
复制代码
|