下面有一段代码,是我在看视频的时候,自己练习的一段代码,但是执行的结果不对,不知道为什么,请大家帮忙啦
#import <Foundation/Foundation.h> @interface Score : NSObject { int _cScore; int _ocScore; } - (void)setCScore:(int)cScore; - (int)cScore; @end
@implementation Score - (void)setCScore:(int)cScore//感觉这里没有执行到 { _cScore = cScore; NSLog(@"设置C的成绩%d",_cScore);//这句话不打印 } - (int)cScore//感觉这里没有执行到 { NSLog(@"取得C的成绩%d",_cScore);//这句话不打印 return _cScore; }
@end
@interface Student : NSObject //组合 { Score *_score; int _age; } - (void)setScore:(int)cScore; - (int)score; @end
@implementation Student - (void)setScore:(int)cScore { [_score setCScore:cScore]; } - (int)score { int cs = [_score cScore]; return cs; } @end
int main() { Student *s = [Student new]; [s setScore:78]; NSLog(@"C语言成绩是:%d",[s score]); return 0;
} 结果: C语言成绩是:0
|