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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 大坏蛋 中级黑马   /  2014-7-20 23:50  /  1043 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


问题是代码里的红色文字,请大家耐心看看,帮我解决一下内心的疑惑
   
/*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 <math.h>
@interface Point2D : NSObject
{
    double _x;
    double _y;
}

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

- (double)y;
- (void)setY : (double)y;

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

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

+ (double)distanceBetweenP1 :(Point2D *)p1 andP2:(Point2D *)p2;
@end


@implementation Point2D

- (double)x
{
    return _x;
}
- (void)setX : (double)x
{
    _x = x;
}

- (double)y
{
    return _y;

}
- (void)setY : (double)y
{
    _y = y;
}

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

    [self setX:x];
    [self setY:y];

}

- (double)distanceWithOther :(Point2D *)p2
{
    Point2D *l =[Point2D new];
    return [Point2D distanceBetweenP1 :l andP2 :p2 ];
/* 或者 return [Point2D distanceBetweenP1 :self  andP2 :p2 ];
   以上两种方式都正确   
   第一种方式 很明显l是个对象   
   第二种方式 使用self 是 Point2D这个类调用distanceBetweenP1: andP2: 这个方法 那么self指向了类  
   那self到底指向谁才对呢  难道说Point2D这个类本身又是自己的对象呢?
   
*/
}

+ (double)distanceBetweenP1 :(Point2D *)p1 andP2:(Point2D *)p2/*根据这个方法可见 很明显第一个参数应该是个对象 可什么self指向类却不报错     
    一定是我没有理解透彻self 求大家帮帮我吧
*/

{

    double m = [p1 x] - [p2 x];
    double n = [p1 y] - [p2 y];
    return sqrt(pow(m,2) + pow(n,2));

}
@end

int main ()
{
    Point2D *p1 = [Point2D new];
    [p1 setX:5 andY:5];
    Point2D *p2 = [Point2D new];
    [p2 setX:12 andY:10];

    double d1 = [p1 distanceWithOther:p2];

    double d2 = [Point2D distanceBetweenP1:p1 andP2:p2];

    NSLog(@"d1=%f,d2=%f",d1,d2);


    return 0;
}


5 个回复

倒序浏览
怎出来表情了  表情是 冒号
回复 使用道具 举报
这个排版格式估计没有几个人会看你的代码
回复 使用道具 举报
istream 发表于 2014-7-21 00:02
这个排版格式估计没有几个人会看你的代码

这个问题 说不清楚只好贴代码了   我好像理解错了  现在明白了
回复 使用道具 举报
编辑问题的时候可以插入代码的。
回复 使用道具 举报
谁调用就是指谁啊。把这句话放心上
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马