每日一练:OC中的属性使用
2018-03-11 22:00
120 查看
// BNRPerson.h#import <Foundation/Foundation.h>@interface BNRPerson : NSObject@property (nonatomic) float heightInMeters;@property (nonatomic) int weightInKilos;//{// //two properties// float _heightInMeters;// int _weightInKilos;//}//methods//- (float)heightInMeters;//- (void)setHeightInMeter:(float)h;//- (int)weightInKiloes;//- (void)setWeightInKilos:(int)w;- (float)bodyMassIndex;@end
// BNRPerson.m#import "BNRPerson.h"
4000
@implementation BNRPerson//- (float)heightInMeters {// return _heightInMeters;//}////- (void)setHeightInMeter:(float)h {// _heightInMeters = h;//}////- (int)weightInKiloes {// return _weightInKilos;//}////- (void)setWeightInKilos:(int)w {// _weightInKilos = w;//}
//@property (nonatomic) float heightInMeters;//@property (nonatomic) int weightInKilos;
- (float)bodyMassIndex {// return _heightInMeters / (_weightInKilos * _weightInKilos); float h = [self weightInKilos]; return [self heightInMeters] / (h * h);}
@end
// BNRStockHolding.h
@implementation BNRStockHolding
//- (float)purchaseSharePrice{
// return _purchaseSharePrice;
//}
//- (float)currentSharePrice{
// return _currentSharePrice;
//}
//- (int)numOfShares{
// return _numOfShares;
//}
//- (void)SetPurchaseSharePrice:(float)pprice {
// _purchaseSharePrice = pprice;
//}
//- (void)SetCurrentSharePrice:(float)cprice {
// _currentSharePrice = cprice;
//}
//- (void)SetNumofShares:(int)nums {
// _numOfShares = nums;
//}
- (float)costInDollars {
return [self purchaseSharePrice] * [self numOfShares];
}
- (float)valueInDollars{
return [self currentSharePrice] * [self numOfShares];
}
@end
// main.m
#import <Foundation/Foundation.h>#import "BNRPerson.h"#import "BNRStockHolding.h"
int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... NSLog(@"Hello, World!"); BNRPerson *tom = [[BNRPerson alloc] init];// tom.heightInMeters = 175;// tom.weightInKilos = 65.0;//// [tom setHeightInMeters:175];// [tom setWeightInKilos:65.0]; tom.heightInMeters = 175; tom.weightInKilos = 65.0; NSLog(@"Tom's height is %f", tom.heightInMeters); NSLog(@"Tom's weight is %d", tom.weightInKilos); // float tomh = [tom heightInMeters];// int tomw = [tom weightInKilos]; float tomh = tom.heightInMeters; int tomw = tom.weightInKilos; NSLog(@"Tom's height in float is %f", tomh); NSLog(@"Tom's weight in int is %d", tomw); float tomBodyIndex = [tom bodyMassIndex]; NSLog(@"Tom's body Index is %f", tomBodyIndex); //stock handling BNRStockHolding *myStocks = [[BNRStockHolding alloc] init]; // @property (nonatomic) float purchaseSharePrice;// @property (nonatomic) float currentSharePrice;// @property (nonatomic) int numOfShares; // [myStocks setPurchaseSharePrice: 22.0];// [myStocks setCurrentSharePrice: 30.0];// [myStocks setNumOfShares: 10000]; myStocks.purchaseSharePrice = 22.0; myStocks.currentSharePrice = 30.0; myStocks.numOfShares = 10000; float currentvalue = [myStocks valueInDollars]; float costvalue = [myStocks costInDollars];// float currentvalue = myStocks.valueInDollars;// float costvalue = myStocks.costInDollars; float myprofit = currentvalue - costvalue; NSLog(@"My profits on %d shares stocks are %f", myStocks.numOfShares, myprofit); NSLog(@"My shares's current price is %f", myStocks.currentSharePrice); } return 0;}
Result:
2018-03-11 21:56:34.362787+0800 TOCClassa[13003:492648] Hello, World!2018-03-11 21:56:34.363057+0800 TOCClassa[13003:492648] Tom's height is 175.0000002018-03-11 21:56:34.363097+0800 TOCClassa[13003:492648] Tom's weight is 652018-03-11 21:56:34.363114+0800 TOCClassa[13003:492648] Tom's height in float is 175.0000002018-03-11 21:56:34.363130+0800 TOCClassa[13003:492648] Tom's weight in int is 652018-03-11 21:56:34.363144+0800 TOCClassa[13003:492648] Tom's body Index is 0.0414202018-03-11 21:56:34.363174+0800 TOCClassa[13003:492648] My profits on 10000 shares stocks are 80000.0000002018-03-11 21:56:34.363190+0800 TOCClassa[13003:492648] My shares's current price is 30.000000Program ended with exit code: 0
// BNRPerson.m#import "BNRPerson.h"
4000
@implementation BNRPerson//- (float)heightInMeters {// return _heightInMeters;//}////- (void)setHeightInMeter:(float)h {// _heightInMeters = h;//}////- (int)weightInKiloes {// return _weightInKilos;//}////- (void)setWeightInKilos:(int)w {// _weightInKilos = w;//}
//@property (nonatomic) float heightInMeters;//@property (nonatomic) int weightInKilos;
- (float)bodyMassIndex {// return _heightInMeters / (_weightInKilos * _weightInKilos); float h = [self weightInKilos]; return [self heightInMeters] / (h * h);}
@end
// BNRStockHolding.h
#import <Foundation/Foundation.h> @interface BNRStockHolding : NSObject @property (nonatomic) float purchaseSharePrice; @property (nonatomic) float currentSharePrice; @property (nonatomic) int numOfShares; //{ // float _purchaseSharePrice; // float _currentSharePrice; // int _numOfShares; //}; //- (float)purchaseSharePrice; //- (float)currentSharePrice; //- (int)numOfShares; //- (void)SetPurchaseSharePrice:(float)pprice; //- (void)SetCurrentSharePrice:(float)cprice; //- (void)SetNumofShares:(int)nums; - (float)costInDollars; - (float)valueInDollars; @end// BNRStockHolding.m#import "BNRStockHolding.h"
@implementation BNRStockHolding
//- (float)purchaseSharePrice{
// return _purchaseSharePrice;
//}
//- (float)currentSharePrice{
// return _currentSharePrice;
//}
//- (int)numOfShares{
// return _numOfShares;
//}
//- (void)SetPurchaseSharePrice:(float)pprice {
// _purchaseSharePrice = pprice;
//}
//- (void)SetCurrentSharePrice:(float)cprice {
// _currentSharePrice = cprice;
//}
//- (void)SetNumofShares:(int)nums {
// _numOfShares = nums;
//}
- (float)costInDollars {
return [self purchaseSharePrice] * [self numOfShares];
}
- (float)valueInDollars{
return [self currentSharePrice] * [self numOfShares];
}
@end
// main.m
#import <Foundation/Foundation.h>#import "BNRPerson.h"#import "BNRStockHolding.h"
int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... NSLog(@"Hello, World!"); BNRPerson *tom = [[BNRPerson alloc] init];// tom.heightInMeters = 175;// tom.weightInKilos = 65.0;//// [tom setHeightInMeters:175];// [tom setWeightInKilos:65.0]; tom.heightInMeters = 175; tom.weightInKilos = 65.0; NSLog(@"Tom's height is %f", tom.heightInMeters); NSLog(@"Tom's weight is %d", tom.weightInKilos); // float tomh = [tom heightInMeters];// int tomw = [tom weightInKilos]; float tomh = tom.heightInMeters; int tomw = tom.weightInKilos; NSLog(@"Tom's height in float is %f", tomh); NSLog(@"Tom's weight in int is %d", tomw); float tomBodyIndex = [tom bodyMassIndex]; NSLog(@"Tom's body Index is %f", tomBodyIndex); //stock handling BNRStockHolding *myStocks = [[BNRStockHolding alloc] init]; // @property (nonatomic) float purchaseSharePrice;// @property (nonatomic) float currentSharePrice;// @property (nonatomic) int numOfShares; // [myStocks setPurchaseSharePrice: 22.0];// [myStocks setCurrentSharePrice: 30.0];// [myStocks setNumOfShares: 10000]; myStocks.purchaseSharePrice = 22.0; myStocks.currentSharePrice = 30.0; myStocks.numOfShares = 10000; float currentvalue = [myStocks valueInDollars]; float costvalue = [myStocks costInDollars];// float currentvalue = myStocks.valueInDollars;// float costvalue = myStocks.costInDollars; float myprofit = currentvalue - costvalue; NSLog(@"My profits on %d shares stocks are %f", myStocks.numOfShares, myprofit); NSLog(@"My shares's current price is %f", myStocks.currentSharePrice); } return 0;}
Result:
2018-03-11 21:56:34.362787+0800 TOCClassa[13003:492648] Hello, World!2018-03-11 21:56:34.363057+0800 TOCClassa[13003:492648] Tom's height is 175.0000002018-03-11 21:56:34.363097+0800 TOCClassa[13003:492648] Tom's weight is 652018-03-11 21:56:34.363114+0800 TOCClassa[13003:492648] Tom's height in float is 175.0000002018-03-11 21:56:34.363130+0800 TOCClassa[13003:492648] Tom's weight in int is 652018-03-11 21:56:34.363144+0800 TOCClassa[13003:492648] Tom's body Index is 0.0414202018-03-11 21:56:34.363174+0800 TOCClassa[13003:492648] My profits on 10000 shares stocks are 80000.0000002018-03-11 21:56:34.363190+0800 TOCClassa[13003:492648] My shares's current price is 30.000000Program ended with exit code: 0
相关文章推荐
- OC中属性和成员变量的区别和使用
- OC常用属性修饰符使用总结
- OC 属性的属性 点语法的使用 KVC
- 每日问题之子类字段属性使用父类的非静态字段属性错误
- OC隐藏和封装-使用点语法访问属性
- UIWebView的使用、oc与js的交互、url的常用属性
- OC中@property属性关键字的使用(assign/weak/strong/copy)
- OC属性property使用
- OC中@property的各种属性的使用详解
- 每日一练:OC中的继承及类扩展的使用
- OC中常用的 关键字 和使用运算符访问属性
- iOS开发OC基础:OC属性的一些概念和基本使用
- OC 【在类实现中如何使用 带下划线的成员变量&属性的点语法】
- oc中成员变量的set/get方法 以及 属性点语法的使用
- FMDB封装,用OC方法存储model,不用写SQL语句。使用runtime获取对象属性
- 04-OC属性的使用、自动释放池、封装和继承
- OC中@property属性关键字的使用(assign/weak/strong/copy)
- animate方法只适用于使用数值设置的css属性
- [iOS] 使用WebViewJavascriptBridge实现OC与JS交互
- android 关于使用SharedPreferences来保存对象里既有属性又有集合的问题