A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© hei军 中级黑马   /  2015-12-3 00:22  /  2650 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

11黑马币

#import <Foundation/Foundation.h>

@interface Expanditure : NSObject{
    double _breakfast;//早餐
    double _lauch;//中餐
    double _dinner;//晚餐
    double _count;//总消费
    double _average;//平均
}

-(void)setBreakfast:(double)breakfast;
-(double)getBreakfast;

-(void)setLauch:(double)lauch;
-(double)getLauch;

-(void)setDinner:(double)dinner;
-(double)getDinner;

-(double)getAverage;

@end

@implementation Expanditure

-(void)setBreakfast:(double)breakfast{
    _breakfast=breakfast;
    _count=0;//这个也必须要有,不然会重复计算
    _count=_breakfast+_lauch+_dinner;

    //如果调用者调用两次会出现重复.不行
   // _count+=_breakfast;

}
-(double)getBreakfast{
    return _breakfast;

}

-(void)setLauch:(double)lauch{
    _lauch=lauch;
    _count=0;
    _count=_breakfast+_lauch+_dinner;


}
-(double)getLauch{
    return _lauch;

}

-(void)setDinner:(double)dinner{
    _dinner=dinner;
    _count=0;
    _count=_breakfast+_lauch+_dinner;


}
-(double)getDinner{
    return _dinner;

}

-(double)getAverage{

    _average=_count/3;
    return _average;

}

@end

int main(){

    Expanditure *ed=[Expanditure new];
    [ed setBreakfast:100.0];
    [ed setLauch:600.0];
    [ed setDinner: 200.0];

    NSLog(@"今天的三餐的平均消费是:%f",[ed getAverage]);
    return 0;
}
}



这是哪个老师讲的,好经典,

最佳答案

查看完整内容

老师讲得 我来教你

4 个回复

倒序浏览
老师讲得  我来教你
回复 使用道具 举报
大家在里边刷一下,有些因为不能显示
回复 使用道具 举报
大神讲得太好了
回复 使用道具 举报
封装(private)
        private特点:
                a: private是一个修饰符
                b: private可以修饰成员变量也可以修饰成员方法
                c: 被private修饰的成员只能在本类中使用
this关键字

        this关键字:        代表的是本类对象的一个引用,谁调用我方法中的this就代表谁.
        this存在的解决的问题:
        就是局部变量隐藏了成员变量的时候,我们可以通过this来明确访问的是成员变量
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马