8、黑马程序员-面向对象的综合练习
来源:互联网 发布:淘宝优惠券转换工具 编辑:程序博客网 时间:2024/06/05 03:56
------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------
一、设计一个类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>@interface point2D : NSObject{ double _x; // x值 double _y; // y值}// x的set和get方法- (void)setX :(double)x;- (double)getX;// y的set和get方法- (void)setY :(double)y;- (double)getY;// 同时设置x y的值- (void)setX:(double)x andY:(double)y;// 计算跟其他点的距离- (double)distanceWithOther:(point2D *)other;// 计算两个点之间的距离+ (double)distanceBettwenPoint:(point2D *)point1 andPoint:(point2D *)point2;@end2、实现#import "point2D.h"#import <math.h>@implementation point2D- (void)setX :(double)x{ _x=x;}- (double)getX{ return _x;}- (void)setY :(double)y{ _y=y;}- (double)getY{ return _y;}- (void)setX:(double)x andY:(double)y{ [self setX:x]; [self setY:y]; }- (double)distanceWithOther:(point2D *)other{ // x1-x2 double xDelta=_x-[other getX]; // (x1-x2)的平方 double xDeltaPF=pow(xDelta, 2); // y1-y2 double yDelta=_y-[other getY]; // (y1-y2)的平方 double yDeltaPF=pow(yDelta, 2); // 返回距离 return sqrt(xDeltaPF+yDeltaPF); //调用类方法 //return [point2D distanceBettwenPoint1:self andPoint:other]; }+ (double)distanceBettwenPoint:(point2D *)point1 andPoint:(point2D *)point2{ // // x1-x2 // double xDelta=[point1 getX]-[point2 getX]; // // // (x1-x2)的平方 // double xDeltaPF=pow(xDelta, 2); // // // y1-y2 // double yDelta=[point1 getY]-[point2 getY]; // // // (y1-y2)的平方 // double yDeltaPF=pow(yDelta, 2); // // // 返回距离 // return sqrt(xDeltaPF+yDeltaPF); return [point1 distanceWithOther:point2]; }@end3、测试#import "point2D.h"int main(int argc, const char * argv[]){ @autoreleasepool { point2D *P1=[point2D new]; [P1 setX:3 andY:4]; point2D *P2=[point2D new]; [P2 setX:6 andY:8]; double d1=[P1 distanceWithOther:P2]; // 计算两点之间的距离 double d2=[point2D distanceBettwenPoint:P1 andPoint:P2]; NSLog(@"之间的距离为%f",d1); NSLog(@"两点之间的距离为%f",d2); // insert code here... NSLog(@"Hello, World!"); } return 0;}------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------
0 0
- 8、黑马程序员-面向对象的综合练习
- 黑马程序员:面向对象设计练习
- 黑马程序员-day18综合练习
- 黑马程序员-day19综合练习
- 黑马程序员-day20综合练习
- 黑马程序员-day21综合练习
- 黑马程序员-day22综合练习
- 黑马程序员-day23综合练习
- 黑马程序员-day24综合练习
- 黑马程序员-day25综合练习
- 黑马程序员 面向对象
- 黑马程序员---面向对象
- 黑马程序员-面向对象
- 黑马程序员-面向对象
- 黑马程序员-面向对象
- 黑马程序员--面向对象
- 黑马程序员-面向对象
- 黑马程序员--面向对象
- poj 2184 Cow Exhibition
- POJ 2263
- 鸟哥的Linux私房菜(基础学习,服务器架设)
- 文件操作 将数据写入所需的文件中
- Android 手动显示和隐藏软键盘
- 8、黑马程序员-面向对象的综合练习
- HDU5120 Intersection(求 圆交)
- 为什么要做持续集成
- 使用struts2自带的json包返回JSON
- JS中关于clientWidth offsetWidth scrollWidth 等的含义
- read/write函数与(非)阻塞I/O的概念
- sguAC50,写一下前面的有价值的题目汇总
- jQuery旋转插件jqueryrotate
- vs2012和vs2010的vc++目录编辑功能已被否决