第四章 继承

Posted liuslayer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第四章 继承相关的知识,希望对你有一定的参考价值。

BankAccounts

技术分享
using System;
using Wrox.ProCSharp;
using Wrox.ProCSharp.VenusBank;
using Wrox.ProCSharp.JupiterBank;
namespace Wrox.ProCSharp
{
   class MainEntryPoint
   {
      static void Main()
      {
         IBankAccount venusAccount = new SaverAccount();
         IBankAccount jupiterAccount = new GoldAccount();
         venusAccount.PayIn(200);
         venusAccount.Withdraw(100);
         Console.WriteLine(venusAccount.ToString());
         jupiterAccount.PayIn(500);
         jupiterAccount.Withdraw(600);
         jupiterAccount.Withdraw(100);
         Console.WriteLine(jupiterAccount.ToString());
      }
   }
}


namespace Wrox.ProCSharp
{
   public interface IBankAccount
   {
      void PayIn(decimal amount);
      bool Withdraw(decimal amount);
      decimal Balance
      {
         get;
      }
   }
}

namespace Wrox.ProCSharp.VenusBank
{
   public class SaverAccount : IBankAccount
   {
      private decimal balance;
      public void PayIn(decimal amount)
      {
         balance += amount;
      }
      public bool Withdraw(decimal amount)
      {
         if (balance >= amount)
         {
            balance -= amount;
            return true;
         }
         Console.WriteLine("Withdrawal attempt failed.");
         return false;
      }
      public decimal Balance
      {
         get
         {
            return balance;
         }
      }
      public override string ToString()
      {
         return String.Format("Venus Bank Saver: Balance = {0,6:C}", balance);
      }
   }
}

namespace Wrox.ProCSharp.JupiterBank
{
   public class GoldAccount : IBankAccount
   {
      private decimal balance;
      public void PayIn(decimal amount)
      {
         balance += amount;
      }
      public bool Withdraw(decimal amount)
      {
         if (balance >= amount)
         {
            balance -= amount;
            return true;
         }
         Console.WriteLine("Withdrawal attempt failed.");
         return false;
      }
      public decimal Balance
      {
         get
         {
            return balance;
         }
      }
      public override string ToString()
      {
         return String.Format("Jupiter Bank Saver: Balance = {0,6:C}", balance);
      }
        }
  
}
View Code

CurrentAccounts

技术分享
using System;
using Wrox.ProCSharp;
using Wrox.ProCSharp.VenusBank;
using Wrox.ProCSharp.JupiterBank;

namespace Wrox.ProCSharp
{
   class MainEntryPoint
   {
      static void Main()
      {
        IBankAccount venusAccount = new SaverAccount();
                ITransferBankAccount jupiterAccount = new CurrentAccount();
                venusAccount.PayIn(200);
                jupiterAccount.PayIn(500);
                jupiterAccount.TransferTo(venusAccount, 100);
                Console.WriteLine(venusAccount.ToString());
                Console.WriteLine(jupiterAccount.ToString());
      }
   }
}


namespace Wrox.ProCSharp
{
   public interface IBankAccount
   {
      void PayIn(decimal amount);
      bool Withdraw(decimal amount);
      decimal Balance
      {
         get;
      }
   }
     
     
     public interface ITransferBankAccount : IBankAccount
   {
      bool TransferTo(IBankAccount destination, decimal amount);
   }
}

namespace Wrox.ProCSharp.VenusBank
{
   public class SaverAccount : IBankAccount
   {
      private decimal balance;
      public void PayIn(decimal amount)
      {
         balance += amount;
      }
      public bool Withdraw(decimal amount)
      {
         if (balance >= amount)
         {
            balance -= amount;
            return true;
         }
         Console.WriteLine("Withdrawal attempt failed.");
         return false;
      }
      public decimal Balance
      {
         get
         {
            return balance;
         }
      }
      public override string ToString()
      {
         return String.Format("Venus Bank Saver: Balance = {0,6:C}", balance);
      }
   }
}



namespace Wrox.ProCSharp.JupiterBank
{
   public class CurrentAccount : ITransferBankAccount
{
   private decimal balance;
   public void PayIn(decimal amount)
   {
      balance += amount;
   }
   public bool Withdraw(decimal amount)
   {
      if (balance >= amount)
      {
         balance -= amount;
         return true;
      }
      Console.WriteLine("Withdrawal attempt failed.");
      return false;
   }
   public decimal Balance
   {
      get
      {
         return balance;
      }
   }
   public bool TransferTo(IBankAccount destination, decimal amount)
   {
      bool result;
      if ((result = Withdraw(amount)) == true)
         destination.PayIn(amount);
      return result;
   }
   public override string ToString()
   {
      return String.Format("Jupiter Bank Current Account: Balance = {0,6:C}",
                                                                      balance);
   }   
  
}


}
View Code

MortimerPhones

技术分享
namespace Wrox.ProCSharp.OOProg
{
   using System;
   

   public abstract class GenericCustomer
   {
      private string name;

      public GenericCustomer()
      {
         name = "<no name>";
      }
      
      public GenericCustomer(string name)
      {
          this.name = name;
      }


      public string Name 
      { 
         get {return name;}
         set {name = value;}
      }

   }

   public class Nevermore60Customer : GenericCustomer
   {
      private string referrerName;
      private uint highCostMinutesUsed;
      
      public Nevermore60Customer(string name) : this(name, "<None>")
      {
      }

      public Nevermore60Customer(string name, string referrerName) : base(name)
      {
         this.referrerName = referrerName;
      }

      public string ReferrerName
      {
         get {return referrerName;}
         set {referrerName = value;}
      }

   }


   public class MainEntryPoint
   {
      public static void Main()
      {
         GenericCustomer arabel = new Nevermore60Customer("Arabel Jones");
      }
   }
}
View Code

 

以上是关于第四章 继承的主要内容,如果未能解决你的问题,请参考以下文章

,继承

Django 第四篇

深入理解DOM节点类型第四篇——文档片段节点DocumentFragment

C++反汇编第四讲,认识多重继承,菱形继承的内存结构,以及反汇编中的表现形式.

java 代码片段

第六周总结&第四次实验报告