您的位置:首页 > 其它

每日一练: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
#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: