C# - 繼承



面向物件程式設計中最重要的概念之一是繼承。繼承允許我們根據另一個類來定義一個類,這使得建立和維護應用程式更加容易。這也提供了重用程式碼功能的機會,並加快了實現時間。

在建立類時,程式設計師可以指定新類應該繼承現有類的成員,而不是編寫全新的資料成員和成員函式。這個現有類稱為基類,新類稱為派生類

繼承的概念實現了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(5);
         Rect.setHeight(7);

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

編譯並執行上述程式碼後,將產生以下結果:

Total area: 35

初始化基類

派生類繼承基類的成員變數和成員方法。因此,在建立子類之前,應該建立父類物件。您可以在成員初始化列表中提供父類初始化的指令。

以下程式演示了這一點:

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();
      }
   }
}

編譯並執行上述程式碼後,將產生以下結果:

Total area: 35
Total paint cost: $2450
廣告