码迷,mamicode.com
首页 > 其他好文 > 详细

03-封装练习

时间:2015-04-26 18:18:52      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:

/*
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;
}

  

03-封装练习

标签:

原文地址:http://www.cnblogs.com/huimotuo/p/4458026.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!