黑马程序员技术交流社区
标题:
oc基础视频中一道题
[打印本页]
作者:
Harvey_ios
时间:
2015-5-8 18:19
标题:
oc基础视频中一道题
设计一个类Point2D,用来表示二维平面的某个点
1> 属性
* double _x;
* double _y;
2> 方法
* 属性相应的set和get方法
* 设计一个对象方法同时设置x和y的值
* 设计一个对象方法计算和其他点的距离
* 设计一个类方法计算两点之间的距离
这方法里面第二点该怎么实现呢?
作者:
大家叫我小祥子
时间:
2015-5-9 00:03
Point2D.h文件中
#import <Foundation/Foundation.h>
@interface Point2D : NSObject
@property double x;
@property double y;
- (void)setPointX:(int)x andPointY: (int)y;
- (float)calculateDistanceWithOther:(Point2D *)point2D;
+ (float)calculatorDistance:(Point2D *)point2D andOther: (Point2D *) other;
@end
复制代码
//
// Point2D.m
// day04
//
// Created by gxiangzi on 15/5/8.
// Copyright (c) 2015年 itcast. All rights reserved.
//
#import "Point2D.h"
#import "math.h"
@implementation Point2D
- (void)setPointX:(int)x andPointY: (int)y
{
self.x = x;
self.y = y;
}
- (float)calculateDistanceWithOther:(Point2D *)point2D
{
float Xdistance = pow((self.x - point2D.x), 2);
float Ydistance = pow((self.y - point2D.y), 2);
return sqrt(Xdistance + Ydistance);
}
+ (float)calculatorDistance:(Point2D *)point2D andOther: (Point2D *) other
{
float Xdistance = pow((point2D.x - other.x), 2);
float Ydistance = pow((point2D.y - other.y), 2);
return sqrt(Xdistance + Ydistance);
}
@end
复制代码
/*
5.设计一个类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进行开根)
*/
#import <Foundation/Foundation.h>
#import "Point2D.h"
int main(int argc, const char * argv[]) {
Point2D * p1 = [[Point2D alloc] init];
Point2D * p2 = [[Point2D alloc] init];
[p1 setPointX:10 andPointY:10];
[p2 setPointX:20 andPointY:20];
float f1 = [p1 calculateDistanceWithOther:p2];
NSLog(@"f1 = %.2f",f1);
float f2 = [Point2D calculatorDistance:p1 andOther:p2];
NSLog(@"f2 = %.2f",f2);
return 0;
}
复制代码
作者:
wangzhewjl
时间:
2015-5-9 09:40
这个是某一个课程的作业吧!
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2