我要在子类内部进行计算。但是我完全陷入困境,因为我无法在子类内部的方法中读取属性self.productNumber。我需要创建方法CalculatePayment。我该如何解决这个问题?
-1
#import <Foundation/Foundation.h>
@interface Product : NSObject
//Property
@property int ProductID;
@property (strong, nonatomic)NSString* productName;
@property float productPrice;
@property (strong, nonatomic)NSString* productMadeInCountry;
//Contructor
-(id) initWithProductID:(int)productID productName:(NSString*)productName productPrice:(float)productPrice productMadeInCountry:(NSString*)productMadeInCountry;
-(id) initWithProductName:(NSString*)productName;
@end
Products.m
#import "Product.h"
@implementation Product
- (id) initWithProductID:(int)productID productName:(NSString*)productName productPrice:(float)productPrice productMadeInCountry:(NSString*)productMadeInCountry
{
self = [super init];
if(self)
{
self.ProductID = productID;
self.productName = productName;
self.productPrice = productPrice;
self.productMadeInCountry = productMadeInCountry;
}
return self;
}
-(id) initWithProductName:(NSString*)productName
{
self = [super init];
if(self)
{
self.productName = productName;
}
return self;
}
@end
Shopping_Cart.h
#import <Foundation/Foundation.h>
#import "Product.h"
@interface Shopping_Cart : NSObject
//property
@property NSMutableArray<Product*>* items;
//Constructor
-(id)initWithItems:(NSMutableArray<Product*>*)items productPrice:(float)productPrice;
//Methods(APIs)
-(void)addItem:(Product*) product;
-(float)calculatePurchase:(Product*) productPrice;
-(void)printProductName:(Product*)productName;
@end