C#繼承


物件導向程式設計中最重要的概念之一是繼承。繼承允許根據另一個類定義一個類,這樣可以更容易地建立和維護應用程式。這也提供了重用程式碼並加快了程式碼的實現。

當建立一個類時,程式員可以指定這個新類應該繼承現有類的哪些成員,而不是編寫繼承全部的資料成員和成員函式。這個現有的類稱為基礎類別,新類稱為派生類或子類。

繼承的想法實現了IS-A(是一個什麼類?)的關係。 例如,哺乳動物IS是動物,狗IS-A哺乳動物,因此也是狗IS-A動物,等等。

基礎類別和派生類

類可以從多個類或介面派生,它可以從多個基礎類別或介面繼承資料和函式。

C# 中建立派生類,可使用如下語法:

<acess-specifier> class <base_class>
{
   ...
}
class <derived_class> : <base_class>
{
   ...
}

考慮有一個基礎類別Shape及其派生類Rectangle,具體繼承的實現詳細如下:

using System;
namespace InheritanceApplication
{
   class Shape 
   {
      public void setWidth(int w)
      {
         width = w;
      }
      public void setHeight(int h)
      {
         height = h;
      }
      protected int width;
      protected int height;
   }

   // Derived class
   class Rectangle: Shape
   {
      public int getArea()
      { 
         return (width * height); 
      }
   }

   class RectangleTester
   {
      static void Main(string[] args)
      {
         Rectangle Rect = new Rectangle();

         Rect.setWidth(10);
         Rect.setHeight(20);

         // Print the area of the object.
         Console.WriteLine("Total area: {0}",  Rect.getArea());
         Console.ReadKey();
      }
   }
}

當編譯和執行上述程式碼時,會產生以下結果:

Total area: 200

初始化基礎類別

派生類繼承基礎類別成員變數和成員方法。 因此,在建立子類之前應該建立超類物件。您可以在成員初始化列表中給出超類初始化的說明或具體實現。

以下程式演示如何實現:

using System;
namespace RectangleApplication
{
   class Rectangle
   {
      //member variables
      protected double length;
      protected double width;
      public Rectangle(double l, double w)
      {
         length = l;
         width = w;
      }

      public double GetArea()
      {
         return length * width;
      }

      public void Display()
      {
         Console.WriteLine("Length: {0}", length);
         Console.WriteLine("Width: {0}", width);
         Console.WriteLine("Area: {0}", GetArea());
      }
   }//end class Rectangle  

   class Tabletop : Rectangle
   {
      private double cost;
      public Tabletop(double l, double w) : base(l, w)
      { }
      public double GetCost()
      {
         double cost;
         cost = GetArea() * 70;
         return cost;
      }
      public void Display()
      {
         base.Display();
         Console.WriteLine("Cost: {0}", GetCost());
      }
   }
   class ExecuteRectangle
   {
      static void Main(string[] args)
      {
         Tabletop t = new Tabletop(4.5, 7.5);
         t.Display();
         Console.ReadLine();
      }
   }
}

當編譯和執行上述程式碼時,會產生以下結果:

Length: 4.5
Width: 7.5
Area: 33.75
Cost: 2362.5

C# 多重繼承

C# 不支援多重繼承。但是,可以使用介面來實現多重繼承。下面範例程式將演示如何實現:

using System;
namespace InheritanceApplication
{
   class Shape 
   {
      public void setWidth(int w)
      {
         width = w;
      }
      public void setHeight(int h)
      {
         height = h;
      }
      protected int width;
      protected int height;
   }

   // Base class PaintCost
   public interface PaintCost 
   {
      int getCost(int area);

   }

   // Derived class
   class Rectangle : Shape, PaintCost
   {
      public int getArea()
      {
         return (width * height);
      }
      public int getCost(int area)
      {
         return area * 70;
      }
   }
   class RectangleTester
   {
      static void Main(string[] args)
      {
         Rectangle Rect = new Rectangle();
         int area;
         Rect.setWidth(5);
         Rect.setHeight(7);
         area = Rect.getArea();

         // Print the area of the object.
         Console.WriteLine("Total area: {0}",  Rect.getArea());
         Console.WriteLine("Total paint cost: ${0}" , Rect.getCost(area));
         Console.ReadKey();
      }
   }
}

當編譯和執行上述程式碼時,會產生以下結果: