我是 Objective CI 的新手,我正在尝试一些示例程序。我不明白 self 和 super 方法在 Objective C 中是如何工作的。在下面的 pgm CashTransaction.m [super trackSpending:amount] 被调用并在 CreditCardTransaction.m [self trackSpending:amount] 被调用。我找不到 self 和 super 之间的区别。super 用于调用基类覆盖方法。self 用于调用子类覆盖方法。这是我的理解。如果有请纠正我我错了。谢谢。
主文件
#import <Foundation/Foundation.h>
#import "BudgetObject.h"
#import "Transaction.h"
#import "CashTransaction.h"
#import "CreditCardTransaction.h"
int main (int argc, const char * argv[]) {
//!---Creating An Object And Allocating It With Values---
Budget* budget = [Budget new];
[budget createBudget:1000.00 withExchangeRate:1.2500];
//!---Declaring And Adding Elements To An Array---
NSMutableArray* transactions = [[NSMutableArray alloc] initWithCapacity:10];
Transaction* aTransaction;
aTransaction = [Transaction new];
[transactions addObject:aTransaction];
//!---Calculating The No Of Elements In An Array---
int k;
k=[transactions count];
NSLog(@"The count value is:%d",k);
//!---Selecting According To The Type Of Transaction---
for(Transaction *iTransaction in transactions){
switch ([aTransaction returnType]) {
case cash:
[budget spendDollars:[iTransaction returnAmount]];
break;
case credit:
[budget changeForeignCurrency:[iTransaction returnAmount]];
break;
default:
break;
}
}
Budget* europeBudget = [Budget new];
[europeBudget createBudget:1000.00 withExchangeRate:1.2500];
Budget* englandBudget = [Budget new];
[englandBudget createBudget:2000.00 withExchangeRate:1.5000];
NSMutableArray* transactions = [[NSMutableArray alloc] initWithCapacity:10];
Transaction* aTransaction;
for(int n=1;n<2;n++){
aTransaction = [CashTransaction new];
[aTransaction createTransaction:n*100 forBudget:europeBudget];
[transactions addObject:aTransaction];
aTransaction = [CashTransaction new];
[aTransaction createTransaction:n*100 forBudget:englandBudget];
[transactions addObject:aTransaction];
}
int n=1;
while (n<4) {
aTransaction = [CreditCardTransaction new];
[aTransaction createTransaction:n*100 forBudget:europeBudget];
[transactions addObject:aTransaction];
aTransaction = [CreditCardTransaction new];
[aTransaction createTransaction:n*100 forBudget:englandBudget];
[transactions addObject:aTransaction];
n++;
}
for(Transaction* aTransaction in transactions){
[aTransaction spend];
}
return 0;
}
预算对象.h
#import <Cocoa/Cocoa.h>
@interface Budget : NSObject {
float exchangeRate;
double budget;
double exchangeTransaction;
}
- (void) createBudget: (double) aBudget withExchangeRate: (float) anExchangeRate;
- (void) spendDollars: (double) dollars;
- (void) changeForeignCurrency: (double) foreignCurrency;
@end
预算对象.m
#import "BudgetObject.h"
@implementation Budget
- (void) createBudget: (double) aBudget withExchangeRate: (float) anExchangeRate;
{
budget = aBudget;
exchangeRate = anExchangeRate;
}
- (void) spendDollars: (double) dollars
{
budget = budget - dollars;
NSLog(@"Converting %0.2f into U.S Foreign Currency leaves $%0.2f",dollars,budget);
}
- (void) changeForeignCurrency: (double) foreignCurrency
{
exchangeTransaction = foreignCurrency * exchangeRate;
budget = budget - exchangeTransaction;
NSLog(@"Charging %0.2f in Foreign Currency leaves $%0.2f",foreignCurrency,budget);
}
@end
事务.h
#import <Cocoa/Cocoa.h>
#import "BudgetObject.h"
@class Budget;
@interface Transaction : NSObject {
Budget* budget;
double amount;
}
- (void) createTransaction: (double) theAmount forBudget: (Budget*) aBudget;
- (void) trackSpending: (double) theAmount;
- (void) spend;
@end
事务.m
#import "Transaction.h"
#import "BudgetObject.h"
@implementation Transaction
- (void) createTransaction: (double) theAmount forBudget: (Budget*) anBudget {
budget = anBudget;
amount = theAmount;
}
- (void) spend {
}
-(void) trackSpending: (double) theAmount {
NSLog(@"You are about to spend another %0.2f",theAmount);
}
@end
现金交易.h
#import <Cocoa/Cocoa.h>
#import "Transaction.h"
@interface CashTransaction : Transaction {
}
@end
现金交易.m
#import "CashTransaction.h"
#import "BudgetObject.h"
@implementation CashTransaction
- (void) spend{
[super trackSpending:amount];
[budget spendDollars:amount];
}
@end
CreditCardTransaction.h
#import <Cocoa/Cocoa.h>
#import "Transaction.h"
@interface CreditCardTransaction : Transaction {
}
@end
信用卡交易.m
#import "CreditCardTransaction.h"
#import "BudgetObject.h"
@implementation CreditCardTransaction
- (void) spend {
[self trackSpending:amount];
[budget changeForeignCurrency:amount];
}
@end
输出:
2011-04-15 11:24:46.112 Bud Obj1[1041:a0f] You are about to spend another 100.00
2011-04-15 11:24:46.114 Bud Obj1[1041:a0f] Converting 100.00 into U.S Foreign Currency leaves $900.00
2011-04-15 11:24:46.115 Bud Obj1[1041:a0f] You are about to spend another 100.00
2011-04-15 11:24:46.115 Bud Obj1[1041:a0f] Converting 100.00 into U.S Foreign Currency leaves $1900.00
2011-04-15 11:24:46.116 Bud Obj1[1041:a0f] You are about to spend another 100.00
2011-04-15 11:24:46.119 Bud Obj1[1041:a0f] Charging 100.00 in Foreign Currency leaves $775.00
2011-04-15 11:24:46.120 Bud Obj1[1041:a0f] You are about to spend another 100.00
2011-04-15 11:24:46.120 Bud Obj1[1041:a0f] Charging 100.00 in Foreign Currency leaves $1750.00
2011-04-15 11:24:46.121 Bud Obj1[1041:a0f] You are about to spend another 200.00
2011-04-15 11:24:46.121 Bud Obj1[1041:a0f] Charging 200.00 in Foreign Currency leaves $525.00
2011-04-15 11:24:46.122 Bud Obj1[1041:a0f] You are about to spend another 200.00
2011-04-15 11:24:46.122 Bud Obj1[1041:a0f] Charging 200.00 in Foreign Currency leaves $1450.00
2011-04-15 11:24:46.123 Bud Obj1[1041:a0f] You are about to spend another 300.00
2011-04-15 11:24:46.123 Bud Obj1[1041:a0f] Charging 300.00 in Foreign Currency leaves $150.00
2011-04-15 11:24:46.124 Bud Obj1[1041:a0f] You are about to spend another 300.00
2011-04-15 11:24:46.125 Bud Obj1[1041:a0f] Charging 300.00 in Foreign Currency leaves $1000.00