#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; } }
这是哪个老师讲的,好经典,
|