A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 池莉娘子 中级黑马   /  2015-7-9 13:59  /  796 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

多文件开发
程序说明:
一.设计一个Point2D类,用来表示2维平面上的点
1.属性
* double _x
* double _y
2.方法
* 属性相应的get和set方法
* 设计一个对象方法同时设置x和y
* 设计一个对象方法计算跟别的点的距离
* 设计一个类方法计算两个点之间的距离
3.提示
* c语言中的math.h中有个函数 double pow(double n,double m);计算n的m次方
* c语言中的math.h中有个函数double sqrt(double n);计算根号n的值

二.设计一个Circle类,表示一个圆
1.属性
* 半径:double _r
* 圆心:Point2D _p
2.方法
* 类方法判断两个圆是否相交,相交返回YES
* 对象方法判断和其他圆是否相交,相交返回YES

在这个程序中就用到了组合,即一个对象中有另一个对象,这就涉及到多文件开发的技巧和原则。
类的独立抽取,就是将.m文件分成.h和.m文件。.h文件中写成员变量和方法的声明,.m文件中写方法的实现。这时候就要注意:一个类需要import另一个类,只能引用这个类的.h文件,如果引用.m文件,会导致重复实现。


程序具体实现:

Point2D.h文件

#import <Foundation/Foundation.h>

@interface Point2D : NSObject
{
        double _x;
        double _y;
}
// x的set和get方法
- (void)setX:(double)x;
- (double)x;

// y的set和get方法
- (void)setY:(double)y;
- (double)y;


// 同时设置x和y的值的对象方法
- (void)setX:(double)x andY:(double)y;

// 计算两点之间的距离
+ (double)distanceWithA:(Point2D *)p1 andB:(Point2D *)p2;

//计算和其他点的距离
- (double)distanceWithOther:(Point2D *)other;

@end


Point2D.m文件
#import "Point2D.h"         // 这里需要拷贝对应的.h文件
#import <math.h>        //拷贝系统文件
@implementaion Point2D

// x的set和get方法
- (void)setX:(double)x;
{
        _x=x;
}
- (double)x;
{
        return _x;
}

// y的set和get方法
- (void)setY:(double)y
{
        _y=y;
}
- (double)y
{
        return _y;
}

// 同时设置x和y的值的对象方法
- (void)setX:(double)x andY:(double)y
{
        _x=x;
        _y=y;
}
// 计算此两点之间的距离
+ (double)distanceWithA:(Point2D *)p1 andB:(Point2D *)p2
{
        double a = pow(double [p1 x]-[p2 x],double 2);
        double b = pow(double [p1 y]-[p2 y],double 2);
        return sqrt(double a+b);
}

//计算和其他点的距离
- (double)distanceWithOther:(Point2D *)other
{
        return [Point2D distanceWithA:self andB:other];        // 调用类方法,其实可以互相调用
}
@end


Circle.h文件

#import <Foundation/Foundation.h>
#import <math.h>
#import "Point2D.h"
@interface Point2D : NSObject
{
        double _r;
        Point2D *_p;
}
// 半径r的set和get方法
- (void)setR:(double)r;
- (double)r;

// 圆心p的set和get方法
- (void)setP:(Point2D *)p;
- (Point2D *)p;


// 判断两圆是否相交,判断方法是:比较圆心距离和半径之和
+ (BOOL)isXiangJiaoWithA:(Circle *)c1 andB:(Circle *)c2

// 判断和其他圆是否相交
- (BOOL)isXiangJiaoWithOther:(Point2D *)other;

@end

Circle.m文件

#import "Circle.h"         // 这里需要拷贝对应的.h文件
#import <math.h>        //拷贝系统文件
@implementaion Circle

// r的set和get方法
- (void)setR:(double)r;
{
        _r=r;
}
- (double)r;
{
        return _r;
}

// 圆心p的set和get方法
- (void)setP:(Point2D *)p
{
        _p=p;
}
- (double)p
{
        return _p;
}

// 判断两圆是否相交,判断方法是:比较圆心距离和半径之和
+ (BOOL)isXiangJiaoWithA:(Circle *)c1 andB:(Circle *)c2
{
        /*
        double a = pow(double [[c1 p] x]-[[c2 p] x],double 2);
        double b = pow(double [[c1 p] y]-[[c2 p] y],double 2);
        double c = sqrt(double a+b);
        */
        double a = [Point2D distanceWithA:[c1 p] andB:[c2 p]];
        double d = c1.r+c2.r;
        return a<d; // 圆心距小于半径之和就是相交
}

// 判断和其他圆是否相交
- (BOOL)isXiangJiaoWithOther:(Point2D *)other
{
        return [Circle isXiangJiaoWithA:self andB:other];
}


@end

main.m文件
#import "Point2D.h"
#import "Circle.h"

int main()
{
        //简单赋值调用
        Point2D *p1 = [Point2D new];
        Point2D *p2 = [Point2D new];
        [p1 setX:2];
        [p1 setY:3];
        [p2 setX:4];
        [p2 setY:5];
        Circle *c1 = [Circle new];
        Circle *c2 = [Circle new];
        c1.p=p1;
        c2.p=p2;
        c1.r=3;
        c2.r=4;
        double d = [Point2D distanceWithA:p1 andB:p2]
        int a = [c1 isXiangJiaoWithOther:c2];
        return 0;

}

3 个回复

倒序浏览
自己发的一些东西,希望没有错的,如果有还请指出来,谢谢
回复 使用道具 举报
要找错误就直接调试运行
回复 使用道具 举报
赞一个!!!!!!!!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马