OC面向对象—封装—练习

//
//  Score.h
//  WenDingdingTest
//
//  Created by LiuChanghong on 15/9/24.
//  Copyright © 2015年 LiuChanghong. All rights reserved.
//

//from http://www.cnblogs.com/wendingding/p/3705219.html
//设计一个成绩类
//这个成绩类有以下四个属性
//OC成绩,可读写
//C成绩,可读写
//总成绩,可读不可写
//平均成绩,可读不可写

#import <Foundation/Foundation.h>

@interface Score : NSObject

{
    CGFloat _ocScore;
    CGFloat _cScore;
    CGFloat _totalScore;
    CGFloat _averageScore;
}

- (void)setOcScore:(CGFloat)ocScore;
- (void)setCScore:(CGFloat)cScore;


- (CGFloat)ocScore;
- (CGFloat)cScore;
- (CGFloat)totalScore;
- (CGFloat)averageScore;

@end
//
//  Score.m
//  WenDingdingTest
//
//  Created by LiuChanghong on 15/9/24.
//  Copyright © 2015年 LiuChanghong. All rights reserved.
//

#import "Score.h"

@implementation Score

-(void)setOcScore:(CGFloat)ocScore{
    _ocScore = ocScore;
}

-(void)setCScore:(CGFloat)cScore{
    _cScore = cScore;
    _totalScore = _ocScore + _cScore;
    _averageScore = _totalScore / 2;
}

-(CGFloat)ocScore{
    return _ocScore;
}

-(CGFloat)cScore{
    return _cScore;
}


-(CGFloat)totalScore{
    return _totalScore;
}

-(CGFloat)averageScore{
    return _averageScore;
}

@end
//
//  main.m
//  WenDingdingTest
//
//  Created by LiuChanghong on 15/9/24.
//  Copyright © 2015年 LiuChanghong. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Score.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        Score *score = [Score new];
        
        [score setOcScore:92.0];
        
        [score setCScore:87.0];
        
        NSLog(@"OC成绩是%.2f",score.ocScore);
        
        NSLog(@"C成绩是%.3f",score.cScore);
        
        NSLog(@"总成绩是%.4f",score.totalScore);
        
        NSLog(@"平均成绩是%.5f",score.averageScore);
        
        
    }
    return 0;
}

输出结果:

posted @ 2015-09-24 21:51  Geeok  阅读(219)  评论(0编辑  收藏  举报