OC 习题:股票K线计算
原创
©著作权归作者所有:来自51CTO博客作者wx58763b21ae3ed的原创作品,请联系作者获取转载授权,否则将追究法律责任
一、设计一个K线类
1)最低价格(可读写(get,set))
2)最高价格(可读写(get,set))
3)平均价格(只读(get) )
1、KLine.h
#import <Foundation/Foundation.h>
@interface KLine : NSObject{
@public
float _highPrice;
float _lowPrice;
float _avgPrice;
}
-(void)setHighPrice:(float) heighPrice;
-(float) getHighPrice;
-(void)setLowPrice:(float) lowPrice;
-(float) getLowPrice;
-(float) getAvgPrice;
@end
2、KLine.m
#import "KLine.h"
@implementation KLine
-(void)setHighPrice:(float) heighPrice{
_highPrice = heighPrice;
_avgPrice =(_lowPrice + _highPrice)/2;
}
-(float) getHighPrice{
return _highPrice;
}
-(void)setLowPrice:(float) lowPrice{
_lowPrice = lowPrice;
_avgPrice =(_lowPrice + _highPrice)/2;
}
-(float) getLowPrice{
return _lowPrice;
}
-(float) getAvgPrice{
//return (_lowPrice + _highPrice)/2;
return _avgPrice;
}
@end
3、main.h:
#import <Foundation/Foundation.h>
#import "KLine.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
KLine *kline = [KLine new];
[kline setHighPrice:30.0f];
[kline setLowPrice:20.0f];
float avg = [kline getAvgPrice];
NSLog(@"avg = %.2f",avg);
}
return 0;
}