这是我的第一个自定义类'实现
第一个标题
#import <Foundation/Foundation.h>
@interface BNRStockHolding : NSObject
@property (nonatomic) float purchaseSharePrice;
@property (nonatomic) float currentSharePrice;
@property (nonatomic) int numberOfShares;
-(float)costInDollars;
-(float)valueInDollars;
@end
首次实施
#import "BNRStockHolding.h"
@implementation BNRStockHolding
-(float)costInDollars{
float cost = [self purchaseSharePrice]*[self numberOfShares];
return cost;
}
-(float)valueInDollars{
float value = [self currentSharePrice]*[self numberOfShares];
return value;
}
@end
子类标题 #进口 #import“BNRStockHolding.h”
@interface BNRForeignStockHolding : BNRStockHolding
@property (nonatomic) float converstionRate;
@end
这是子类'implementation
#import "BNRForeignStockHolding.h"
@implementation BNRForeignStockHolding
-(float)costInDollars{
float normalCost = [super costInDollars];
return normalCost*[self converstionRate];
}
-(float)valueInDollars{
float normalValue = [super valueInDollars];
return normalValue*[self converstionRate];
}
@end
主
#import <Foundation/Foundation.h>
#import "BNRForeignStockHolding.h"
int main
(int argc, const char * argv[])
{
@autoreleasepool {
BNRStockHolding *AAPL = [[BNRStockHolding alloc]init];
BNRStockHolding *GOOG = [[BNRStockHolding alloc]init];
BNRStockHolding *NYSE = [[BNRStockHolding alloc]init];
NSMutableArray *stockArray = [NSMutableArray array];
AAPL.purchaseSharePrice=2.30;
AAPL.currentSharePrice=4.50;
AAPL.numberOfShares=40;
[stockArray addObject:AAPL];
GOOG.purchaseSharePrice=12.19;
GOOG.currentSharePrice=10.56;
GOOG.numberOfShares=90;
[stockArray addObject:GOOG];
NYSE.purchaseSharePrice=45.10;
NYSE.currentSharePrice=49.51;
NYSE.numberOfShares=210;
[stockArray addObject:NYSE];
BNRForeignStockHolding *conv = [[BNRForeignStockHolding alloc]init];
conv.converstionRate = 0.5;
for (BNRForeignStockHolding *d in stockArray)
{
float currentTotalPrice = d.costInDollars;
float purchasedTotalPrice = d.valueInDollars;
NSLog(@"Purchased:$%f \n\t\t\t\t\t\t\t\t\t\t Valued:$%f",currentTotalPrice, purchasedTotalPrice);
}
}
return 0;
}
我正在尝试覆盖子类.m中的方法costInDollars和valueInDollars,同时实现对float的conversionRate。
我的主要是从原始类中获取方法,而不是子类,并且它们没有被覆盖。
抱歉 - 谢谢。答案 0 :(得分:0)
简单地将每种类型转换为BNRForeignStockHolding
不会改变它们的具体类型。在提供的代码中,您只需向NSArray添加BNRStockHolding
个实例。你可以在增强的for循环中通过编译器的原因是因为动态绑定 - NSArray没有任何类型的知识,因此编译器只能假设它们充满了id。允许您在运行时访问costInDollars
的原因是因为基类碰巧响应该选择器,但它们在运行时不是BNRForeignStockHolding
类型。
如果您需要这些结果,则需要将所有[[BNRStockHolding alloc] init]
更改为[[BNRForeignStockHolding alloc] init]
并在每个实例上设置conversionRate
。
<强>替代强>
如果conversionRate
可以是全局的,那么您可以添加类getter / setter。
实施例。在BNRForeignStockHolding.m:
static _conversionRate;
+ (void)setConversionRate:(float)conversionRate {
_conversionRate = conversionRate;
}
+ (float)conversionRate {
return _conversionRate;
}
然后在实例方法中:
-(float)costInDollars{
float normalCost = [super costInDollars];
return normalCost*[self.class conversionRate];
}
-(float)valueInDollars{
float normalValue = [super valueInDollars];
return normalValue*[self.class conversionRate];
}
并在您的实施中:
[BNRForeignStockHolding setConversionRate:0.5];