Objective CI は初めてで、いくつかのサンプル プログラムを試しています。self メソッドと super メソッドが Objective C でどのように機能するか理解できませんでした。 trackSpending:amount] が呼び出されます。self と super.super の違いが見つかりませんでした。super は、基本クラスのオーバーライドされたメソッドを呼び出すためのものです。self は、子クラスのオーバーライドされたメソッドを呼び出すためのものです。これが私の理解です。私は間違っています。よろしくお願いします。
main.m
#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;
}
BudgetObject.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
BudgetObject.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
CashTransaction.h
#import <Cocoa/Cocoa.h>
#import "Transaction.h"
@interface CashTransaction : Transaction {
}
@end
CashTransaction.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
CreditCardTransaction.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