------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net
培训</a>、期待与您交流! -------
通过oc基本知识的学习,在看懂老师点与圆习题编程方法的基础上,通过自己的理解重写程序,并在编程的过程中不断总结与回顾学过的知识点,达到温故而知新的目的。
在xcode编程的过程中,碰到不少的问题,因为xcode有实时编译的功能,因此能及时作出改正,主要代码都作了注释,方便自己与别人日后查看。
习题:圆的设计。
1、设计一个Poing2D类,包含double x,double y两个成员变量。
2、设计x,y的getter与setter,设计一个对象方法同时设置x和y,设计一个对象方法计算跟其他点的距离,设计一个对象方法计算两点之间的距离。
3、math.h中的两个函数,double pow(double n, double m),计算n的m次方,double sqrt(double n),计算根号n的值。
4、设计一个Circle类,用来表示二维平面中的圆。包含 _radius, *point两个成员变量。
5、设置_radiu与*point的setter与getter,设计一个对象方法判断跟其他圆是否重叠,设计一个对象方法判断两个圆是否重叠。
程序如下:
#import<Foundation/Foundation.h>
#import<math.h>
@interface Point2D : NSObject //创建一个二维点对象,类的生命
{
double _x; // x轴
double _y; // y轴
}
// x与y的setter
- (void)setX:(double)x;
- (void)setY:(double)y;
- (void)setX:(double)x andSetY:(double)y; // 同时设置x与y
// x与y的getter
- (double)x;
- (double)y;
- (double)distanceWithOtherPoint:(Point2D *)other; // 该点与另一点的距离
- (double)distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2; // 两点间的距离
@end
@implementation Point2D // 类的实现
- (void)setX:(double)x
{
_x = x;
}
- (void)setY:(double)y
{
_y = y;
}
- (double)x
{
return _x;
}
- (double)y
{
return _y;
}
- (double)distanceWithOtherPoint:(Point2D *)other
{
// double xDistance = [self x] - [other x];
// double yDistance = [self y] - [other y];
// double pfh = pow(xDistance, 2) + pow(yDistance,2);
// return sqrt(pfh);
return [self distanceBetweenPoint1:self andPoint2:other];
}
- (double)distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2;
{
double xDistance = [p1 x] - [p2 x]; // 两点x方向的距离
double yDistance = [p1 y] - [p2 y]; // 两点y方向多距离
double pfh = pow(xDistance, 2) + pow(yDistance,2); // 两点距离差的平方
return sqrt(pfh); // 距离平方开根号即距离作为返回值
}
- (void)setX:(double)x andSetY:(double)y
{
[self setX:x]; //等同于 self->_x = x; _x = x;
[self setY:y];
}
@end
@interface Circle: NSObject // circle类的声明
{
double _radius;
Point2D *_point; //组合
}
- (void)setRadius:(double)radius;
- (double)radius;
- (void)setPiont:(Point2D *)point;
- (Point2D *)point;
- (BOOL)isCircleCompleWithOtherCircle:(Circle *)circle;
- (BOOL)isCircle1:(Circle *)c1 compleWithCircle2:(Circle *)c2;
@end
@implementation Circle // circle类的实现
- (void)setRadius:(double)radius
{
_radius = radius;
}
- (double)radius
{
return _radius;
}
- (void)setPiont:(Point2D *)point
{
_point = point;
}
- (Point2D *)point
{
return _point;
}
- (BOOL)isCircleCompleWithOtherCircle:(Circle *)circle
{
// Point2D *p1 = [self point];
// Point2D *p2 = [circle point];
// double distance = [p1 distanceWithOtherPoint:p2];
// double pointDistance = [self radius] + [circle radius];
// return distance < pointDistance;
return [self isCircle1:self compleWithCircle2:circle];
}
- (BOOL)isCircle1:(Circle *)c1 compleWithCircle2:(Circle *)c2
{
Point2D *p1 = [c1 point];
Point2D *p2 = [c2 point];
double distance = [p1 distanceWithOtherPoint:p2];
double pointDistance = [c1 radius]+[c2 radius];
return distance < pointDistance;
}
@end
int main()
{
Point2D *p1 = [Point2D new];
Point2D *p2 = [Point2D new];
[p1 setX:5 andSetY:5];
[p2 setX:5 andSetY:5];
Circle *c1 = [Circle new];
Circle *c2 = [Circle new];
[c1 setPiont:p1];
[c2 setPiont:p2];
[c1 setRadius:3];
[c2 setRadius:4];
BOOL i = [c1 isCircleCompleWithOtherCircle:c2]; // 重叠返回1,不重叠返回0
NSLog(@"%d",i);
return 0;
}