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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© meijinyu 中级黑马   /  2014-11-30 20:27  /  1411 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

#import<Foundation/Foundation.h>

typedef enum {
    ColorRed,
    ColorGreen,
    ColorGlack

} Color;

typedef enum {
    SexWomen,
    SexMan

} Sex;

@interface Dog :NSObject
{
    @public
    Color color;
    int speed;
    Sex sex;
    double weight;
}
- (void)eat;
- (void)bark;
- (void)run;
- (BOOL)isSameColorWithOther:(Dog *)other;
- (int)compareSpeedWithOther:(Dog *)other;
@end

@implementation Dog
- (void)eat
{
    weight+=0.5;
    NSLog(@"吃完后的体重为:%f",weight);
}

- (void)bark
{
    NSLog(@"颜色为:%d,速度为:%d,性别为:%d,体重为:%f",color,speed,sex,weight);
   
}

- (void)run
{
    weight-=0.5;
    NSLog(@"狗跑的速度为%dkm/h,跑完后的体重为:%f",speed,weight);
}

- (BOOL)isSameColorWithOther:(Dog *)other;
{
    return color-other->color;
}

- (int)compareSpeedWithOther:(Dog *)other
{
    return speed==other->speed;
}
@end

int main()
{
    Dog *p=[Dog new];
   
    [p bark];
   
    [p eat];
   
    [p eat];
   
    return 0;
}


这段代码中,- (BOOL)isSameColorWithOther:(Dog *)other;
这句代码怎么解释啊?

5 个回复

倒序浏览
是这一句- (BOOL)isSameColorWithOther:(Dog *)other;
回复 使用道具 举报
表情是:(),不知道怎么就转化成表情了
回复 使用道具 举报
1.返回BOOL类型的值
2.参数为Dog类的对象

作用:判断与其他狗的颜色是否相同
另外,implementation中,你这个方法的实现多了个分号
  1. - (BOOL)isSameColorWithOtherDog *)other;   // 这里多了个分号
  2. {
  3.     return color-other->color;
  4. }
复制代码
回复 使用道具 举报
Jr_711 发表于 2014-11-30 21:24
1.返回BOOL类型的值
2.参数为Dog类的对象

你好,我就是你说的第二点我看不懂,你能说细一点吗?谢谢了
回复 使用道具 举报
meijinyu 发表于 2014-11-30 22:45
你好,我就是你说的第二点我看不懂,你能说细一点吗?谢谢了

Dog *d1 = [Dog new];
Dog *d2 = [Dog new];

BOOL  b = [d1 isSameColorWithOther:d2];     //比较d1和d2的颜色是不是一样,,应该能看懂吧
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马