OC之综合小练习

 

/*

 设计一个类Point2D,表示二维平面中的某个点

 1> 属性

 * double _x

 * double _y

 

 2> 方法

 * 属性相应的setget方法

 * 设计一个对象方法同时设置xy

 * 设计一个对象方法计算跟其他点的距离

 * 设计一个类方法计算两个点之间的距离

 */

 

#import <Foundation/Foundation.h>

#import <math.h>

 

// 

@interface Point2D : NSObject

{

    double _x;

    double _y;

}

 

- (void)setX:(double)x;

- (double)x;

 

- (void)setY:(double)y;

- (double)y;

 

// 同时设置xy

- (void)setX:(double)x andY:(double)y;

 

// 设计一个对象方法计算跟其他点的距离

- (double)distanceWithOther:(Point2D *)other;

// 设计一个类方法计算两个点之间的距离

+ (double)distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2;

 

@end

 

 

@implementation Point2D

- (void)setX:(double)x

{

    _x = x;

}

- (double)x

{

    return _x;

}

 

- (void)setY:(double)y

{

    _y = y;

}

- (double)y

{

    return _y;

}

 

// 同时设置xy

- (void)setX:(double)x andY:(double)y

{

    /*

     _x = x;

    _y = y;

     */

    /*

     self->_x = x;

     self->_y = y;

     */

    // 推荐做法

    [self setX:x];

    [self setY:y];

    /*

      获取成员变量_x3种方式

      _x;   // 直接访问

      self->_x;   // 利用self直接访问

      [self x];    // 调用getter,最好的方式

     */

}

 

// 设计一个对象方法计算跟其他点的距离

- (double)distanceWithOther:(Point2D *)other

{

    // 在对象方法里 调用 类方法     在类方法中调用对象方法也可以实现

    return [Point2D distanceBetweenPoint1:self andPoint2:other];

}

// 设计一个类方法计算两个点之间的距离

+ (double)distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2

{

    // ( (x1 - x2)的平方 + (y1 - y2)的平方 ) 开跟

    // (x1 - x2)

    double xCha = [p1 x] - [p2 x];

    // (x1 - x2)的平方

    double xChaPF = pow(xCha, 2);

    // (y1 - y2)

    double yCha = [p1 y] - [p2 y];

    // (y1 - y2)的平方

    double yChaPF = pow(yCha, 2);

    

    return sqrt(xChaPF + yChaPF);

    

}

@end

 

/*

 设计一个类Circle,表示二维平面中的圆

 1> 属性

  * double _radius (半径)

  * Point2D *_point (圆心)

 

 2> 方法

  * 属性相应的gettersetter

  * 设计一个对象方法,判断与其他圆是否相交(相交返回YES,否则返回NO

  * 设计一个类方法,判断两个圆是否相交(相交返回YES,否则返回NO

 */

// 

@interface Circle : NSObject

{

    double _radius;   // 半径

    Point2D *_point;   // 圆心    此次不宜用继承,而是用组合

}

 

// 半径的gettersetter

- (void)setRadius:(double)radius;

- (double)radius;

 

// 圆心的settergetter

- (void)setPoint:(Point2D *)point;

- (Point2D *)point;

 

// 设计一个对象方法,判断与其他圆是否相交(相交返回YES,否则返回NO

// 返回值是BOOL类型的,方法名一般都以is开头

- (BOOL)isIntersectWithOther:(Circle *)other;

// 设计一个类方法,判断两个圆是否相交(相交返回YES,否则返回NO

// 返回值是BOOL类型的,方法名一般都以is开头

+ (BOOL)isIntersectBetweenCircle1:(Circle *)c1 andCircle2:(Circle *)c2;

 

@end

 

@implementation Circle

// 半径的gettersetter

- (void)setRadius:(double)radius

{

    _radius = radius;

}

- (double)radius

{

    return _radius;

}

 

// 圆心的settergetter

- (void)setPoint:(Point2D *)point

{

    _point = point;

}

- (Point2D *)point

{

    return _point;

}

 

// 设计一个对象方法,判断与其他圆是否相交(相交返回YES,否则返回NO

// 返回值是BOOL类型的,方法名一般都以is开头

- (BOOL)isIntersectWithOther:(Circle *)other

{

    

     // 如果两个圆的圆心距离 >= 半径和,不相交;否则相交

    Point2D *p1 = [self point];

    Point2D *p2 = [other point];

   

    double disPoint = [p1 distanceWithOther:p2];   // 两个圆心之间的距离

    NSLog(@"圆心之间的距离为:%f", disPoint);

    

    double heRadius = [self radius] + [other radius];

    NSLog(@"半径和为:%f", heRadius);

    /*

    if(disPoint < heRadius)  // 相交

    {

        return YES;

    }

    return NO;

     */

    // return (disPoint < heRadius) ? YES : NO;

    return disPoint < heRadius;

    

}

// 设计一个类方法,判断两个圆是否相交(相交返回YES,否则返回NO

// 返回值是BOOL类型的,方法名一般都以is开头

+ (BOOL)isIntersectBetweenCircle1:(Circle *)c1 andCircle2:(Circle *)c2

{

    // 在类中调用对象方法

    return [c1 isIntersectWithOther:c2];

}

 

@end

 

int main()

{

    void testCircle();  // 方法声明

    testCircle();    // 方法调用

    

    void testPoint2D();

    //testPoint2D();

    

    return 0;

}

// 验证Circle相关方法是否正确

void testCircle()

{

    // 创建圆对象

    Circle *c1 = [Circle new];

    // 设置圆的半径

    [c1 setRadius:3];

    // 创建点对象作为圆心

    Point2D *p1 = [Point2D new];

    // 设置点的xy

    [p1 setX:1 andY:1];

    // 设置点p1作为圆c1的圆心

    [c1 setPoint:p1];

   

    // 创建圆对象

    Circle *c2 = [Circle new];

    // 设置圆的半径

    [c2 setRadius:6];

    // 创建点对象作为圆心

    Point2D *p2 = [Point2D new];

    // 设置点的xy

    [p2 setX:55 andY:66];

    // 设置点p1作为圆c1的圆心

    [c2 setPoint:p2];

    

    // 判断两个圆是否相交

    BOOL result = [c1 isIntersectWithOther:c2];

    NSLog(@"相交吗?---%d", result);

    BOOL result2 = [Circle isIntersectBetweenCircle1:c1 andCircle2:c2];

    NSLog(@"相交吗?---%d", result2);

}

 

// 验证Point2D相关方法是否正确

void testPoint2D()

{

    Point2D *p1 = [Point2D new];

    [p1 setX:2 andY:2];

    

    Point2D *p2 = [Point2D new];

    [p2 setX:5 andY:6];

    

    // double dis = [p1 distanceWithOther:p2];

    double dis = [Point2D distanceBetweenPoint1:p1 andPoint2:p2];

    NSLog(@"dis is %f", dis);

}

posted on 2015-04-08 08:51  我是一匹小黑马  阅读(146)  评论(0编辑  收藏  举报