Deposit类型直接依赖于Transaction与TransactionType类型,所以在Deposit的编译期间,必须确保可访问到这两者的程序集。但是,编译器可能会发出一个警告,表示TransactionType已经被引入了两次,一次是直接,而另一次是间接地通过Transaction,在此,可安全地忽略此警告信息。
Withdrawal类定义在例4中,而Transfer类定义在例5中。
例4:
| using namespace System; public ref class Withdrawal sealed : Transaction { Decimal amount; int fromAccount; public: Withdrawal(double amount, int fromAccount) : Transaction(TransactionType::Withdrawal) { WithdrawalAmount = Decimal(amount); WithdrawalFromAccount = fromAccount; } Withdrawal(Decimal amount, int fromAccount) : Transaction(TransactionType::Withdrawal) { WithdrawalAmount = amount; WithdrawalFromAccount = fromAccount; } property Decimal WithdrawalAmount { Decimal get() { return amount; }; private: void set(Decimal value) { amount = value; }; } property int WithdrawalFromAccount { int get() { return fromAccount; }; private: void set(int value) { fromAccount = value; }; } void PostTransaction() { Console::WriteLine("{0} -- {1}", DateTimeOfTransaction, this); } virtual String^ ToString() override { return String::Format("With: {0,10:0.00} {1,10}", WithdrawalAmount, WithdrawalFromAccount); } }; |
例5:
| using namespace System; public ref class Transfer sealed : Transaction { Decimal amount; int fromAccount; int toAccount; public: Transfer(double amount, int fromAccount, int toAccount): Transaction(TransactionType::Transfer) { TransferAmount = Decimal(amount); TransferFromAccount = fromAccount; TransferToAccount = toAccount; } Transfer(Decimal amount, int fromAccount, int toAccount): Transaction(TransactionType::Transfer) { TransferAmount = amount; TransferFromAccount = fromAccount; TransferToAccount = toAccount; } property Decimal TransferAmount { Decimal get() { return amount; }; private: void set(Decimal value) { amount = value; }; } property int TransferFromAccount { int get() { return fromAccount; }; private: void set(int value) { fromAccount = value; }; } property int TransferToAccount { int get() { return toAccount; }; private: void set(int value) { toAccount = value; }; } void Transfer::PostTransaction() { Console::WriteLine("{0} -- {1}", DateTimeOfTransaction, this); } virtual String^ ToString() override { return String::Format("Xfer: {0,10:0.00} {1,10} {2,10}", TransferAmount, TransferToAccount, TransferFromAccount); } }; |
虽然三个PostTransaction的实现是同样的,但在真实的交易处理系统中,这是不可能发生的。

