Chinaunix首页 | 论坛 | 博客
  • 博客访问: 101920787
  • 博文数量: 19283
  • 博客积分: 9968
  • 博客等级: 上将
  • 技术积分: 196062
  • 用 户 组: 普通用户
  • 注册时间: 2007-02-07 14:28
文章分类

全部博文(19283)

文章存档

2011年(1)

2009年(125)

2008年(19094)

2007年(63)

分类: C/C++

2008-05-18 20:47:36

  来源:


  标号5中的格式指定符{0,10:0.00},表明在10个打印位宽度中右对齐数额,并四舍五入到小数点后两位,且至少在小数点前有一位数。

  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的实现是同样的,但在真实的处理系统中,这是不可能发生的。

  测试程序

  例6是测试交易类型的程序,它会创建一个具体交易类型的数组、遍历此数组、调用每个元素的PostTransaction函数。插1是某次执行后的输出,默认使用的是美国式的时间格式,即为,月、日、年、12小时制。

  例6:

using namespace System;

int main()
{
 array^ list = gcnew array {
  gcnew Deposit(123.05, 12345),
  gcnew Transfer(Decimal::Parse("1256.40"), 1111, 222),
  gcnew Withdrawal(34.54, 232323),
  gcnew Deposit(56.12, 14321)
 };
 for each (Transaction^ t in list)
 {
  t->PostTransaction();
 }
}

  插1:例6某次执行后的输出

3/20/2005 12:36:16 AM -- Dep: 123.05 12345
3/20/2005 12:36:18 AM -- Xfer: 1256.40 222 1111
3/20/2005 12:36:19 AM -- With: 34.54 232323
3/20/2005 12:36:21 AM -- Dep: 56.12 14321
阅读(291) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~