标签:
/* 4.设计一个成绩类 * C语言成绩(可读可写) * OC成绩(可读可写) * 总分(只读) * 平均分(只读) */ #import <Foundation/Foundation.h> @interface Score : NSObject { int _cScore; // C语言成绩 int _ocScore; // OC成绩 int _totalScore;// 总分 int _averageScoe; // 平均分 } - (void)setCScore:(int)cScore; - (int)cScore; - (void)setOcScore:(int)ocScore; - (int)ocScore; - (int)totalScore; - (int)averageScore; @end @implementation Score - (void)setCScore:(int)cScore { _cScore = cScore; // 计算总分 _totalScore = _cScore + _ocScore; _averageScoe = _totalScore/2; } - (int)cScore { return _cScore; } - (void)setOcScore:(int)ocScore { _ocScore = ocScore; // 计算总分 _totalScore = _cScore + _ocScore; _averageScoe = _totalScore/2; } // 监听成员变量的改变 - (int)ocScore { return _ocScore; } - (int)totalScore { return _totalScore; } - (int)averageScore { return _averageScoe; } @end int main() { Score *s = [Score new]; [s setCScore:90]; [s setOcScore:100]; [s setCScore:80]; int a = [s totalScore]; NSLog(@"总分:%d", a); return 0; }
标签:
原文地址:http://www.cnblogs.com/huimotuo/p/4458026.html