视频上老师讲解组合一带而过,感觉应该不难,可能设置变量的时候就蒙了
#import <Foundation/Foundation.h>
@interface Score : NSObject
{
int _cScore;
}
- (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];
Score *score = [Score new];
[score setCScore:100];
[s setScore:78];
NSLog(@"C语言成绩是:%d",[s score]);
return 0;
}
看到论坛里一同学发过这样的帖子,说原因是因为另一个类没有实例化,是在要main函数里用Score创建对象吗? |
|