每日一练:OC中的属性使用


//  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"


@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.000000

2018-03-11 21:56:34.363097+0800 TOCClassa[13003:492648] Tom's weight is 65

2018-03-11 21:56:34.363114+0800 TOCClassa[13003:492648] Tom's height in float is 175.000000

2018-03-11 21:56:34.363130+0800 TOCClassa[13003:492648] Tom's weight in int is 65

2018-03-11 21:56:34.363144+0800 TOCClassa[13003:492648] Tom's body Index is 0.041420

2018-03-11 21:56:34.363174+0800 TOCClassa[13003:492648] My profits on 10000 shares stocks are 80000.000000

2018-03-11 21:56:34.363190+0800 TOCClassa[13003:492648] My shares's current price is 30.000000

Program ended with exit code: 0




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值