草随原

骏马

导航

接口的第二个实例

using System;
using System.Collections.Generic;
using System.Text;
//-----------------------------------------------第一段
using Wrox.ProCSharp.JupiterBank;
namespace Wrox.ProCSharp
{
      public inte***ce IBankAccount //Account 帐目
      {
          void PayIn(decimal amount);
          bool Withdraw(decimal amount); //amount 总数
          decimal Balance
          {
              get;
          }
      }
//-----------------------------------------------第二段
class MainEntryPoint
{
      static void Main()
      {
          IBankAccount jupiterAccount = new GoldAccount(); //jupiter 木星
          jupiterAccount.PayIn(500);
          jupiterAccount.Withdraw(600);
          jupiterAccount.Withdraw(100);
          Console.WriteLine(jupiterAccount.ToString());
      }
}
}
//-----------------------------------------------第三段
namespace Wrox.ProCSharp.JupiterBank

{
      public class GoldAccount : IBankAccount
      {
          private decimal balance;

          public void PayIn(decimal amount)
          {
              balance += amount;
          }
//---------------------------------------------3.1
          public bool Withdraw(decimal amount)
          {
              if (balance >= amount)
              {
                  balance -= amount;
                  return true;
              }
              Console.WriteLine("Withdrawal attempt failed.");
              return false;
          }
//---------------------------------------------3.2
          public decimal Balance
          {
              get
              {
                  return balance;
              }

          }
//---------------------------------------------3.3
          public override string ToString()
          {
              return String.Format("Jupiter Bank Saver: Balance = {0,6:C}", balance);
          }
      }
}

posted on 2007-03-24 10:46  淄衣  阅读(152)  评论(2)    收藏  举报