• 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;
    }

    输出结果:

  • 相关阅读:
    map内置函数、lambda表达式、快捷生成想要的列表、filter内置函数
    python生成随机验证码
    Redis数据库之概念与创建服务
    JavaScript中的类
    python之with的使用
    PHP变量名区分大小写,函数名不区分大小写
    php curl基本操作
    PHP生成随机字符串包括大小写字母
    PHP多例模式
    一个关于动态编译时 Evidence的问题
  • 原文地址:https://www.cnblogs.com/liuchanghong/p/4836698.html
Copyright © 2020-2023  润新知