黑马程序员技术交流社区
标题:
oc两个类之间的方法调用
[打印本页]
作者:
77媛儿
时间:
2014-3-21 21:03
标题:
oc两个类之间的方法调用
本帖最后由 77媛儿 于 2014-3-24 13:52 编辑
//分数类
#import <Foundation/Foundation.h>
@interface Scord:NSObject
{
@public
int cScord;
int ocScord;
int iosScord;
}
- (int)compare_cScordWithother:(Scord *)other;
- (int)compare_ocScordWithother:(Scord *)other;
- (int)compare_iosScordWithother:(Scord *)other;
- (int)sumAll;
- (int)average;
@end
@implementation Scord
- (int)compare_cScordWithother:(Scord *)other
{
return cScord-other->cScord;
}
- (int)compare_ocScordWithother:(Scord *)other
{
return ocScord-other->ocScord;
}
- (int)compare_iosScordWithother:(Scord *)other
{
return iosScord-other->iosScord;
}
- (int)sumAll
{
return cScord+ocScord+iosScord;
}
- (int)average
{
return (cScord+ocScord+iosScord)/3;
}
@end
//学生类
@interface Student:NSObject
{
@public
char *name;
int number;
Scord *s;
}
- (int)compare_cScordWithother:(Student *)other;
@end
@implementation Student
- (int)compare_cScordWithother:(Student *)other
{
return [s compare_iosScordWithother:other->s];
}
@end
int main()
{
Student *a=[Student new];
Scord *s1=[Scord new];
a->name="jack";
a->number=22;
a->s=s1;
s1->cScord=90;
Student *a1=[Student new];
Scord *s2=[Scord new];
a1->name="rose";
a1->number=23;
a1->s=s2;
s2->cScord=92;
int b=[a compare_cScordWithother:a1];
NSLog(@"%d",b);
}
输出结果不正确,求帮助
复制代码
作者:
周宇华
时间:
2014-3-21 21:48
代码51行错了,改为:
return [s compare_cScordWithother:other->s];
复制代码
这个函数是计算C语言的分数,你调到ios分数了,结果自然就不正确了。
作者:
lixiangzhou
时间:
2014-3-21 23:02
楼上正解,我试了一下,也是51行出错,学生类调用的是 C ,而内部实现却是 IOS,
把 compare_iosScordWithother 改为 compare_cScordWithother 即可
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2