zoukankan      html  css  js  c++  java
  • 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;
    }

    输出结果:

  • 相关阅读:
    BZOJ1251: 序列终结者
    BZOJ1014 [JSOI2008]火星人prefix
    NOI模拟赛Day6
    NOI模拟赛Day5
    BZOJ2329: [HNOI2011]括号修复
    NOI模拟赛Day4
    状压dp题目总结
    BZOJ2097[Usaco2010 Dec] 奶牛健美操
    BZOJ4027: [HEOI2015]兔子与樱花 贪心
    BZOJ1443: [JSOI2009]游戏Game
  • 原文地址:https://www.cnblogs.com/liuchanghong/p/4836698.html
Copyright © 2011-2022 走看看