每日一练:OC中的继承及类扩展的使用
2018-03-12 23:06
204 查看
我们在Employee 加入类扩展。 实现属性和方法的私有化。
//// BNRPerson.h// TOCClassa//
#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// TOCClassa//
#import "BNRPerson.h"
@interface BNRPerson ()- (BOOL)hasDriverLicence;@end@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;
- (BOOL)hasDriverLicence { return false;}- (float)bodyMassIndex {// return _heightInMeters / (_weightInKilos * _weightInKilos); float h = [self weightInKilos]; return [self heightInMeters] / (h * h);}
@end
//// BNRStockHolding.h// TOCClassa//
#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// TOCClassa//
//
#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
//// BNREmployee.h// TOCClassa//
//
#import "BNRPerson.h"@class BNRAsset;
@interface BNREmployee : BNRPerson//{// NSMutableArray *_assets;//};@property (nonatomic) unsigned int employeeId;@property (nonatomic) NSDate *hireDate;@property (nonatomic,copy) NSArray *assets;- (double)yearOfEmployment;- (void)addAsset:(BNRAsset *)a;- (unsigned int)valueOfAssets;@end
//// BNREmployee.m// TOCClassa//
#import "BNREmployee.h"#import "BNRAsset.h"
@interface BNREmployee (){ NSMutableArray *_assets;};@property (nonatomic) unsigned int officeAlarmCode;
- (void)fly;
@end
@implementation BNREmployee//@property (nonatomic) unsigned int employeeId;//@property (nonatomic) unsigned int officeAlarmCode;//@property (nonatomic) NSDate *hireDate;
- (void)setAssets:(NSArray *)a { _assets = [a mutableCopy];}
- (NSArray *)assets { return [_assets copy];}
- (void)addAsset:(BNRAsset *)a { if(!_assets){ _assets = [[NSMutableArray alloc]init]; } [_assets addObject:a];}
- (unsigned int)valueOfAssets { unsigned int sum = 0; for(BNRAsset *a in _assets){ sum += [a resaleValue]; } return sum;}
- (double)yearOfEmployment { if(self.hireDate) { NSDate *now = [NSDate date]; NSTimeInterval secs = [now timeIntervalSinceDate:self.hireDate]; return secs / 31557600.0; } else { return 0; }}//重写父类的方法- (float)bodyMassIndex{// return 19.0; float empbodyIndex = [super bodyMassIndex]; return empbodyIndex * 0.9;}
- (void)fly{ NSLog(@"This is for class extention use");}//重写父类的description方法(NSObject)- (NSString *)description {// return [NSString stringWithFormat:@"<Employee id:%d >",self.employeeId]; self.officeAlarmCode = 1002; NSLog(@"The employee's office alarm code is :%d", self.officeAlarmCode); [self fly]; return [NSString stringWithFormat:@"<Employee %u: $%u in assets>", self.employeeId, self.valueOfAssets];}
- (void)dealloc { NSLog(@"deallocaing %@", self);}
@end
//// BNRAsset.h// TOCClassa//
//#import "BNRPerson.h"#import <Foundation/Foundation.h>
@interface BNRAsset : NSObject@property (nonatomic, copy) NSString *label;@property (nonatomic) unsigned int resaleValue;@end
//// BNRAsset.m// TOCClassa//
#import "BNRAsset.h"
@implementation BNRAsset- (NSString *)description { return [NSString stringWithFormat:@"<%@ : $%u>",self.label, self.resaleValue];}- (void)dealloc { NSLog(@"deallocation %@", self);}@end
//// main.m// TOCClassa////
#import <Foundation/Foundation.h>//#import "BNRPerson.h"#import "BNREmployee.h"#import "BNRStockHolding.h"#import "BNRAsset.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); //functions to Employee now. BNREmployee *mike = [[BNREmployee alloc] init]; mike.weightInKilos = 55; mike.heightInMeters = 180; NSString *hireDateStr = @"2010-07-19"; NSDateFormatter *df = [[NSDateFormatter alloc]init]; [df setDateFormat:@"YYYY-MM-DD"]; NSDate *hiredatea = [df dateFromString:hireDateStr]; mike.hireDate = hiredatea; double yearofworks = [mike yearOfEmployment]; NSLog(@"Mike's work employment is %f", yearofworks); mike.employeeId = 10020; NSLog(@"Mike is as following: %@", mike); // mike.officeAlarmCode = 2002; // NSLog(@"Mike's employee id is %d and his office alarm code is %d", mike.employeeId, mike.officeAlarmCode);//override bodyMassIndex method//重构 NSLog(@"Mike's body index is %f", mike.bodyMassIndex); NSLog(@"%@ hired date is %@", mike, mike.hireDate); //add new Asset class //create a new array to store employees NSMutableArray *employees = [[NSMutableArray alloc]init]; for(int i = 0; i < 10; i++){ BNREmployee *jim = [[BNREmployee alloc]init]; //set values to new instance jim.weightInKilos = 55 + i; jim.heightInMeters = 1.8 - i / 10.0; jim.employeeId = i; [employees addObject:jim]; } //create 10 assets for (int i = 0; i < 10; i++){ //create a asset object BNRAsset *asset = [[BNRAsset alloc] init]; NSString *currentLabel = [NSString stringWithFormat:@"Laptop %d", i]; asset.label = currentLabel; asset.resaleValue = 350 + i * 17; //generate the ramdom numbers between 0 to 9 NSUInteger randomNum = random() % [employees count]; //get the employee object BNREmployee *randomEmployee = [employees objectAtIndex:randomNum]; [randomEmployee addAsset:asset]; } NSLog(@"Employee: %@", employees); NSLog(@"Giving up ownership of one employee");// NSUInteger a = 5;// [employees removeObject:a]; NSLog(@"Giving up ownership of arrays"); employees = nil; } return 0;}
Result:
2018-03-12 23:02:03.178920+0800 TOCClassa[43583:2514538] Hello, World!2018-03-12 23:02:03.179236+0800 TOCClassa[43583:2514538] Tom's height is 175.0000002018-03-12 23:02:03.179281+0800 TOCClassa[43583:2514538] Tom's weight is 652018-03-12 23:02:03.179300+0800 TOCClassa[43583:2514538] Tom's height in float is 175.0000002018-03-12 23:02:03.179317+0800 TOCClassa[43583:2514538] Tom's weight in int is 652018-03-12 23:02:03.179333+0800 TOCClassa[43583:2514538] Tom's body Index is 0.0414202018-03-12 23:02:03.179365+0800 TOCClassa[43583:2514538] My profits on 10000 shares stocks are 80000.0000002018-03-12 23:02:03.179379+0800 TOCClassa[43583:2514538] My shares's current price is 30.0000002018-03-12 23:02:03.183148+0800 TOCClassa[43583:2514538] Mike's work employment is 8.1449962018-03-12 23:02:03.183231+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.183250+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.183308+0800 TOCClassa[43583:2514538] Mike is as following: <Employee 10020: $0 in assets>2018-03-12 23:02:03.183340+0800 TOCClassa[43583:2514538] Mike's body index is 0.0535542018-03-12 23:02:03.183419+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.183464+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.183551+0800 TOCClassa[43583:2514538] <Employee 10020: $0 in assets> hired date is Tue Jan 19 00:00:00 20102018-03-12 23:02:03.183795+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.183821+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.183883+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.183906+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.183952+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.183983+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.184018+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.184037+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.184070+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.184087+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.184125+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.184143+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.184186+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.184223+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.184266+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.184288+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.184322+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.184333+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.184373+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.184392+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.184464+0800 TOCClassa[43583:2514538] Employee: ( "<Employee 0: $0 in assets>", "<Employee 1: $503 in assets>", "<Employee 2: $469 in assets>", "<Employee 3: $768 in assets>", "<Employee 4: $0 in assets>", "<Employee 5: $836 in assets>", "<Employee 6: $819 in assets>", "<Employee 7: $384 in assets>", "<Employee 8: $0 in assets>", "<Employee 9: $486 in assets>")2018-03-12 23:02:03.184488+0800 TOCClassa[43583:2514538] Giving up ownership of one employee2018-03-12 23:02:03.184497+0800 TOCClassa[43583:2514538] Giving up ownership of arrays2018-03-12 23:02:03.184536+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.184548+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.184570+0800 TOCClassa[43583:2514538] deallocaing <Employee 0: $0 in assets>2018-03-12 23:02:03.184593+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.184623+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.184688+0800 TOCClassa[43583:2514538] deallocaing <Employee 1: $503 in assets>2018-03-12 23:02:03.184781+0800 TOCClassa[43583:2514538] deallocation <Laptop 9 : $503>2018-03-12 23:02:03.184808+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.184824+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.184840+0800 TOCClassa[43583:2514538] deallocaing <Employee 2: $469 in assets>2018-03-12 23:02:03.184910+0800 TOCClassa[43583:2514538] deallocation <Laptop 7 : $469>2018-03-12 23:02:03.184935+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.184952+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.184973+0800 TOCClassa[43583:2514538] deallocaing <Employee 3: $768 in assets>2018-03-12 23:02:03.184998+0800 TOCClassa[43583:2514538] deallocation <Laptop 0 : $350>2018-03-12 23:02:03.185019+0800 TOCClassa[43583:2514538] deallocation <Laptop 4 : $418>2018-03-12 23:02:03.185030+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.185040+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.185059+0800 TOCClassa[43583:2514538] deallocaing <Employee 4: $0 in assets>2018-03-12 23:02:03.185072+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.185080+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.185092+0800 TOCClassa[43583:2514538] deallocaing <Employee 5: $836 in assets>2018-03-12 23:02:03.185254+0800 TOCClassa[43583:2514538] deallocation <Laptop 3 : $401>2018-03-12 23:02:03.185285+0800 TOCClassa[43583:2514538] deallocation <Laptop 5 : $435>2018-03-12 23:02:03.185306+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.185321+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.185341+0800 TOCClassa[43583:2514538] deallocaing <Employee 6: $819 in assets>2018-03-12 23:02:03.185367+0800 TOCClassa[43583:2514538] deallocation <Laptop 1 : $367>2018-03-12 23:02:03.185401+0800 TOCClassa[43583:2514538] deallocation <Laptop 6 : $452>2018-03-12 23:02:03.185440+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.185459+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.185496+0800 TOCClassa[43583:2514538] deallocaing <Employee 7: $384 in assets>2018-03-12 23:02:03.185525+0800 TOCClassa[43583:2514538] deallocation <Laptop 2 : $384>2018-03-12 23:02:03.185545+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.185560+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.185581+0800 TOCClassa[43583:2514538] deallocaing <Employee 8: $0 in assets>2018-03-12 23:02:03.185599+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.185613+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.185633+0800 TOCClassa[43583:2514538] deallocaing <Employee 9: $486 in assets>2018-03-12 23:02:03.185660+0800 TOCClassa[43583:2514538] deallocation <Laptop 8 : $486>2018-03-12 23:02:03.185771+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.185793+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.185817+0800 TOCClassa[43583:2514538] deallocaing <Employee 10020: $0 in assets>Program ended with exit code: 0
//// BNRPerson.h// TOCClassa//
#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// TOCClassa//
#import "BNRPerson.h"
@interface BNRPerson ()- (BOOL)hasDriverLicence;@end@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;
- (BOOL)hasDriverLicence { return false;}- (float)bodyMassIndex {// return _heightInMeters / (_weightInKilos * _weightInKilos); float h = [self weightInKilos]; return [self heightInMeters] / (h * h);}
@end
//// BNRStockHolding.h// TOCClassa//
#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// TOCClassa//
//
#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
//// BNREmployee.h// TOCClassa//
//
#import "BNRPerson.h"@class BNRAsset;
@interface BNREmployee : BNRPerson//{// NSMutableArray *_assets;//};@property (nonatomic) unsigned int employeeId;@property (nonatomic) NSDate *hireDate;@property (nonatomic,copy) NSArray *assets;- (double)yearOfEmployment;- (void)addAsset:(BNRAsset *)a;- (unsigned int)valueOfAssets;@end
//// BNREmployee.m// TOCClassa//
#import "BNREmployee.h"#import "BNRAsset.h"
@interface BNREmployee (){ NSMutableArray *_assets;};@property (nonatomic) unsigned int officeAlarmCode;
- (void)fly;
@end
@implementation BNREmployee//@property (nonatomic) unsigned int employeeId;//@property (nonatomic) unsigned int officeAlarmCode;//@property (nonatomic) NSDate *hireDate;
- (void)setAssets:(NSArray *)a { _assets = [a mutableCopy];}
- (NSArray *)assets { return [_assets copy];}
- (void)addAsset:(BNRAsset *)a { if(!_assets){ _assets = [[NSMutableArray alloc]init]; } [_assets addObject:a];}
- (unsigned int)valueOfAssets { unsigned int sum = 0; for(BNRAsset *a in _assets){ sum += [a resaleValue]; } return sum;}
- (double)yearOfEmployment { if(self.hireDate) { NSDate *now = [NSDate date]; NSTimeInterval secs = [now timeIntervalSinceDate:self.hireDate]; return secs / 31557600.0; } else { return 0; }}//重写父类的方法- (float)bodyMassIndex{// return 19.0; float empbodyIndex = [super bodyMassIndex]; return empbodyIndex * 0.9;}
- (void)fly{ NSLog(@"This is for class extention use");}//重写父类的description方法(NSObject)- (NSString *)description {// return [NSString stringWithFormat:@"<Employee id:%d >",self.employeeId]; self.officeAlarmCode = 1002; NSLog(@"The employee's office alarm code is :%d", self.officeAlarmCode); [self fly]; return [NSString stringWithFormat:@"<Employee %u: $%u in assets>", self.employeeId, self.valueOfAssets];}
- (void)dealloc { NSLog(@"deallocaing %@", self);}
@end
//// BNRAsset.h// TOCClassa//
//#import "BNRPerson.h"#import <Foundation/Foundation.h>
@interface BNRAsset : NSObject@property (nonatomic, copy) NSString *label;@property (nonatomic) unsigned int resaleValue;@end
//// BNRAsset.m// TOCClassa//
#import "BNRAsset.h"
@implementation BNRAsset- (NSString *)description { return [NSString stringWithFormat:@"<%@ : $%u>",self.label, self.resaleValue];}- (void)dealloc { NSLog(@"deallocation %@", self);}@end
//// main.m// TOCClassa////
#import <Foundation/Foundation.h>//#import "BNRPerson.h"#import "BNREmployee.h"#import "BNRStockHolding.h"#import "BNRAsset.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); //functions to Employee now. BNREmployee *mike = [[BNREmployee alloc] init]; mike.weightInKilos = 55; mike.heightInMeters = 180; NSString *hireDateStr = @"2010-07-19"; NSDateFormatter *df = [[NSDateFormatter alloc]init]; [df setDateFormat:@"YYYY-MM-DD"]; NSDate *hiredatea = [df dateFromString:hireDateStr]; mike.hireDate = hiredatea; double yearofworks = [mike yearOfEmployment]; NSLog(@"Mike's work employment is %f", yearofworks); mike.employeeId = 10020; NSLog(@"Mike is as following: %@", mike); // mike.officeAlarmCode = 2002; // NSLog(@"Mike's employee id is %d and his office alarm code is %d", mike.employeeId, mike.officeAlarmCode);//override bodyMassIndex method//重构 NSLog(@"Mike's body index is %f", mike.bodyMassIndex); NSLog(@"%@ hired date is %@", mike, mike.hireDate); //add new Asset class //create a new array to store employees NSMutableArray *employees = [[NSMutableArray alloc]init]; for(int i = 0; i < 10; i++){ BNREmployee *jim = [[BNREmployee alloc]init]; //set values to new instance jim.weightInKilos = 55 + i; jim.heightInMeters = 1.8 - i / 10.0; jim.employeeId = i; [employees addObject:jim]; } //create 10 assets for (int i = 0; i < 10; i++){ //create a asset object BNRAsset *asset = [[BNRAsset alloc] init]; NSString *currentLabel = [NSString stringWithFormat:@"Laptop %d", i]; asset.label = currentLabel; asset.resaleValue = 350 + i * 17; //generate the ramdom numbers between 0 to 9 NSUInteger randomNum = random() % [employees count]; //get the employee object BNREmployee *randomEmployee = [employees objectAtIndex:randomNum]; [randomEmployee addAsset:asset]; } NSLog(@"Employee: %@", employees); NSLog(@"Giving up ownership of one employee");// NSUInteger a = 5;// [employees removeObject:a]; NSLog(@"Giving up ownership of arrays"); employees = nil; } return 0;}
Result:
2018-03-12 23:02:03.178920+0800 TOCClassa[43583:2514538] Hello, World!2018-03-12 23:02:03.179236+0800 TOCClassa[43583:2514538] Tom's height is 175.0000002018-03-12 23:02:03.179281+0800 TOCClassa[43583:2514538] Tom's weight is 652018-03-12 23:02:03.179300+0800 TOCClassa[43583:2514538] Tom's height in float is 175.0000002018-03-12 23:02:03.179317+0800 TOCClassa[43583:2514538] Tom's weight in int is 652018-03-12 23:02:03.179333+0800 TOCClassa[43583:2514538] Tom's body Index is 0.0414202018-03-12 23:02:03.179365+0800 TOCClassa[43583:2514538] My profits on 10000 shares stocks are 80000.0000002018-03-12 23:02:03.179379+0800 TOCClassa[43583:2514538] My shares's current price is 30.0000002018-03-12 23:02:03.183148+0800 TOCClassa[43583:2514538] Mike's work employment is 8.1449962018-03-12 23:02:03.183231+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.183250+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.183308+0800 TOCClassa[43583:2514538] Mike is as following: <Employee 10020: $0 in assets>2018-03-12 23:02:03.183340+0800 TOCClassa[43583:2514538] Mike's body index is 0.0535542018-03-12 23:02:03.183419+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.183464+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.183551+0800 TOCClassa[43583:2514538] <Employee 10020: $0 in assets> hired date is Tue Jan 19 00:00:00 20102018-03-12 23:02:03.183795+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.183821+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.183883+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.183906+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.183952+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.183983+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.184018+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.184037+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.184070+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.184087+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.184125+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.184143+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.184186+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.184223+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.184266+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.184288+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.184322+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.184333+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.184373+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.184392+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.184464+0800 TOCClassa[43583:2514538] Employee: ( "<Employee 0: $0 in assets>", "<Employee 1: $503 in assets>", "<Employee 2: $469 in assets>", "<Employee 3: $768 in assets>", "<Employee 4: $0 in assets>", "<Employee 5: $836 in assets>", "<Employee 6: $819 in assets>", "<Employee 7: $384 in assets>", "<Employee 8: $0 in assets>", "<Employee 9: $486 in assets>")2018-03-12 23:02:03.184488+0800 TOCClassa[43583:2514538] Giving up ownership of one employee2018-03-12 23:02:03.184497+0800 TOCClassa[43583:2514538] Giving up ownership of arrays2018-03-12 23:02:03.184536+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.184548+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.184570+0800 TOCClassa[43583:2514538] deallocaing <Employee 0: $0 in assets>2018-03-12 23:02:03.184593+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.184623+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.184688+0800 TOCClassa[43583:2514538] deallocaing <Employee 1: $503 in assets>2018-03-12 23:02:03.184781+0800 TOCClassa[43583:2514538] deallocation <Laptop 9 : $503>2018-03-12 23:02:03.184808+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.184824+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.184840+0800 TOCClassa[43583:2514538] deallocaing <Employee 2: $469 in assets>2018-03-12 23:02:03.184910+0800 TOCClassa[43583:2514538] deallocation <Laptop 7 : $469>2018-03-12 23:02:03.184935+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.184952+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.184973+0800 TOCClassa[43583:2514538] deallocaing <Employee 3: $768 in assets>2018-03-12 23:02:03.184998+0800 TOCClassa[43583:2514538] deallocation <Laptop 0 : $350>2018-03-12 23:02:03.185019+0800 TOCClassa[43583:2514538] deallocation <Laptop 4 : $418>2018-03-12 23:02:03.185030+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.185040+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.185059+0800 TOCClassa[43583:2514538] deallocaing <Employee 4: $0 in assets>2018-03-12 23:02:03.185072+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.185080+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.185092+0800 TOCClassa[43583:2514538] deallocaing <Employee 5: $836 in assets>2018-03-12 23:02:03.185254+0800 TOCClassa[43583:2514538] deallocation <Laptop 3 : $401>2018-03-12 23:02:03.185285+0800 TOCClassa[43583:2514538] deallocation <Laptop 5 : $435>2018-03-12 23:02:03.185306+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.185321+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.185341+0800 TOCClassa[43583:2514538] deallocaing <Employee 6: $819 in assets>2018-03-12 23:02:03.185367+0800 TOCClassa[43583:2514538] deallocation <Laptop 1 : $367>2018-03-12 23:02:03.185401+0800 TOCClassa[43583:2514538] deallocation <Laptop 6 : $452>2018-03-12 23:02:03.185440+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.185459+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.185496+0800 TOCClassa[43583:2514538] deallocaing <Employee 7: $384 in assets>2018-03-12 23:02:03.185525+0800 TOCClassa[43583:2514538] deallocation <Laptop 2 : $384>2018-03-12 23:02:03.185545+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.185560+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.185581+0800 TOCClassa[43583:2514538] deallocaing <Employee 8: $0 in assets>2018-03-12 23:02:03.185599+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.185613+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.185633+0800 TOCClassa[43583:2514538] deallocaing <Employee 9: $486 in assets>2018-03-12 23:02:03.185660+0800 TOCClassa[43583:2514538] deallocation <Laptop 8 : $486>2018-03-12 23:02:03.185771+0800 TOCClassa[43583:2514538] The employee's office alarm code is :10022018-03-12 23:02:03.185793+0800 TOCClassa[43583:2514538] This is for class extention use2018-03-12 23:02:03.185817+0800 TOCClassa[43583:2514538] deallocaing <Employee 10020: $0 in assets>Program ended with exit code: 0
相关文章推荐
- Objective-C,如何在不使用类继承的情况下,扩展已有类的实现
- OC学习之道:OC中类别--Category类目的使用/Eetension类的扩展 的使用
- C++入门经典 笔记 (第十六章)使用继承扩展类
- MVC扩展控制器工厂,通过继承DefaultControllerFactory来决定使用哪个接口实现,使用Ninject
- oc中分类 扩展 协议的使用及区别
- 失败的尝试,使用继承扩展数组,以及ES6的必要性
- OC中SEL,类别,继承,协议的使用
- MVC扩展Filter,通过继承HandleErrorAttribute,使用log4net或ELMAH组件记录服务端500错误、HttpException、Ajax异常等
- OC中类目(Catagory)和扩展(Extension)的使用
- 每日一练:OC中的继承
- 如何使用Createjs来编写HTML5游戏(八) 通过继承扩展EaselJS中的基础元素
- OC中SEL,类别,继承,协议的使用
- oc中分类 扩展 协议的使用及区别
- OC继承详解与使用规则
- 我的OC练习(五)-@property的使用和子类继承的练习
- 使用继承进行类扩展新功能,真的好吗?
- 使用Ext实现js的继承,以及扩展Ext组件的思路
- 小白学开发(iOS)OC_ 使用继承来扩充类(2015-08-07)
- 每日一练:OC中的属性使用
- OC_self的调用和使用注意,继承的基本使用和使用注意,多态笔记