- #import <Foundation/Foundation.h>
- @interface Score : NSObject
- {
- int _cScore;
- int _ocScore;
- int _iOSScore;
- }
- - (void)setCScore:(int)newCScore;
- - (int)cScore;
- - (int)ocScore;
- - (int)iOSScore;
- - (void)setOcScore:(int)newOcScore;
- - (void)setIosScore:(int)newIosScore;
- - (void)addScore:(Score *)Xscore add :(int) num;
- @end
- //score方法实现
- @implementation Score
- - (void)setCScore:(int)newCScore
- {
- _cScore = newCScore;
- NSLog(@"已经给c成绩赋值_cScore:%d",_cScore);
- }
- - (int)cScore
- {
- return _cScore;
- }
- - (int)ocScore
- {
- return _ocScore;
- }
- - (int)iOSScore
- {
- return _iOSScore;
- }
- - (void)setOcScore:(int)newOcScore
- {
- _ocScore = newOcScore;
- NSLog(@"已经给oc成绩赋值:_ocscore=%d",_ocScore);
- }
- - (void)setIosScore:(int)newIosScore
- {
- _iOSScore = newIosScore;
- NSLog(@"已经给ios成绩赋值:_iOSScore=%d",_iOSScore);
-
- }
- <font color="#ff0000">- (void)addScore:(Score *)Xscore add :(int) num
- {
- switch (Xscore) {
- case cscore:
- _cScore += num;
- break;
- case ocscore:
- _ocScore += num;
- break;
- case iosscore:
- _iOSScore += num;
- break;
- default:
- break;
- }</font>
- }
- @end
- @interface Student : NSObject
- {
- Score *_score;//成绩。组合
- }
- - (void)setScore:(Score *)newScore;
- - (Score *)score;
- - (void)study;//每学习一次,3可成绩各加1分,输出学习完后的3科成绩
- @end
- //学生类的方法实现
- @implementation Student
- - (void)setScore:(Score *)newScore
- {
- _score = newScore;//给成绩赋值
- NSLog(@"现在给score赋值:cscore:%d-ocscore:%d-iosscore:%d",[_score cScore],[_score ocScore],[_score iOSScore]);
-
- }
- - (Score *)score//返回成绩
- {
- return _score;
- }
- - (void)study//每学习一次,3可成绩各加1分,输出学习完后的3科成绩
- {
- NSLog(@"现在c语言成绩:%d,+1",[_score cScore]);
- <font color="#ff0000"> [_score addScore:cscore add :1];</font>
- NSLog(@"现在oc成绩:%d,+1",[_score ocScore]);
- <font color="#ff0000"> [_score addScore:ocscore add :1];</font>
- NSLog(@"现在ios成绩:%d,+1",[_score iOSScore]);
- <font color="#ff0000"> [_score addScore:iosscore add :1];</font>
- NSLog(@"学习完,三科成绩为:_cScore=%d,_ocScore=%d,_iOSScore=%d",[_score cScore],[_score ocScore],[_score iOSScore]);
-
- }
- @end
- int main()
- {
- Student *stu = [Student new];
- Score *s = [Score new];
- [s setCScore:80];
- [s setOcScore:79];
- [s setIosScore:91];
- [stu setScore:s];
- [stu study];
- return 0;
- }
复制代码 我想用同一个方法实现分别对三门功课成绩+1对操作,想传一个分数名称过去,请问我该咋写?
|
|