OC类的练习


学了一段时间的类,了解了面向对象的思想和方法,通过一些练习加强、深化知识,这里有两道练习题,包含了面向对象的思想和方法
第一题:

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

 1>属性

 * double _x

 * double _y

  2>方法

 * 属性相应的setget方法

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

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

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

第二题:

 设计一个类Cricle,用来表示二维平面中的圆

 1>属性

 * double _radius (半径)

 * Point2D *_point (圆心)

 方法

 * 属性相应的setget方法

 * 设计一个对象判断跟其他圆是否重叠(重叠返回YES,否则返回NO

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

其实这两个题有很大的联系,圆心就是点

下面是Point2D)类代码

#import <Foundation/Foundation.h>
#import <math.h>

@interface Point2D : NSObject
{
    double _x; // 点的x值
    double _y; // 点的y值
}
// 相应的set和get方法
- (void)setX:(double)x;
- (double)x;
- (void)setY:(double)y;
- (double)y;
//设计一个对象方法同时设置x和y
- (void)setXWith:(double)x andY:(double)y;

//计算跟其他点的距离
- (double)distanceWithOtherPiont:(Point2D *)other;
//计算两点之间的距离
+ (double)distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2;
@end

@implementation Point2D
// 实现相应的set和get方法
- (void)setX:(double)x
{
    _x = x;
}
- (double)x;
{
    return _x;
}
- (void)setY:(double)y
{
    _y = y;
}
- (double)y;
{
    return _y;
}
//实现对象方法同时设置x和y
- (void)setXWith:(double)x andY:(double)y
{
    [self setX:x];
    [self setY:y];
}

//实现计算跟其他点的距离
- (double)distanceWithOtherPiont:(Point2D *)other
{
    return [Point2D distanceBetweenPoint1:self andPoint2:other]; //调用了<span style="font-family: Arial, Helvetica, sans-serif;">+ (double)distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2方法</span>

}
//实现计算两点之间的距离
+ (double)distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2
{
    //x的差值
    double Xdifference = [p1 x] - [p2 x];
    //x差的平方
    double differencePfX = pow(Xdifference,2);
    //y的差值
    double Ydifference = [p1 y] - [p2 y];
    //y的差值平方
    double differencePfY = pow(Ydifference,2);
    //两点之间的距离
    return sqrt(differencePfX+differencePfY);
    
}
@end
因为用到了求平方和开根号,所以要导入math.h头文件,熟练运用self可以使代码更加清晰,代码相似的地方可以用方法的调用简化代码

下面是圆(Cricle)类代码

//类:圆(Cricle)
@interface Cricle : NSObject
{
    double _radius; // 半径
    Point2D *_point; // 圆心
}
// 设置半径的set和get方法
- (void)setRadius:(double)raduis;
- (double)raduis;
//设置圆心的set和get方法
- (void)setPoint:(Point2D *)point;
- (Point2D *)point;

// 对象判断跟其他圆是否重叠
- (BOOL)isInteractWithOther:(Cricle *)other;
//类方法判断两个圆是否重叠
+ (BOOL)isInteractBetweenCricle1:(Cricle *)cricle1 WithCricle2:(Cricle *)cricle2;

@end

@implementation Cricle
- (void)setRadius:(double)raduis
{
    _radius = raduis;
}
- (double)raduis
{
    return _radius;
}

- (void)setPoint:(Point2D *)point
{
    _point = point;
}
- (Point2D *)point
{
    return _point;
}

//对象判断跟其他圆是否重叠
- (BOOL)isInteractWithOther:(Cricle *)other
{
    //获得圆心
    Point2D *p1 = [self point];
    Point2D *p2 = [other point];
    //圆心距离
    double distance = [p1 distanceWithOtherPiont:p2]; // 直接调用Point2D的类方法
    //两圆半径和
    double distanceSum = [self raduis] + [other raduis];
    
    return distance < distanceSum; //注意布尔型的返回值
}
// 判断两个圆是否重叠
+ (BOOL)isInteractBetweenCricle1:(Cricle *)cricle1 WithCricle2:(Cricle *)cricle2 
{
    return [cricle1 isInteractWithOther:cricle2]; // 调用了<span style="font-family: Arial, Helvetica, sans-serif;">- (BOOL)isInteractWithOther:(Cricle *)other方法</span>

}

@end
Cricle类用到了Point2D类的方法,此场合不适合继承,所以用到了类的组合


这两个看似不大的练习题包含了很多知识点,通过不断的练习提高自己的能力

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值