C# 的繼承(學習心得 19)

繼承允許我們根據一個類來定義另一個類,這使得創建和維護應用程序變得更容易,利於重用代碼和節省開發時間。

已有的類被稱爲的 基類,這個新的類被稱爲 派生類

繼承的思想實現了 屬於(IS-A) 關係。

例如,哺乳動物 屬於(IS-A) 動物,狗 屬於(IS-A) 哺乳動物,因此狗 屬於(IS-A) 動物。

一、基類和派生類

一個類可以派生自 多個 類或接口。

語法:

<訪問修飾符符> class <基類>
{
 ...
}
class <派生類> : <基類>
{
 ...

例:

using System;
namespace InheritanceApplication
{
   class Shape
   {
      public void setWidth(int w)
      {
         width = w;
      }
      public void setHeight(int h)
      {
         height = h;
      }
      protected int width; 
     // protected:只有該類對象及其子類對象可以訪問
     // 屬性定義在方法後面也行
      protected int height;
   }

   // 派生類
   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);

         // 打印對象的面積
         Console.WriteLine("總面積: {0}",  Rect.getArea());
        // 使用派生類的方法
         Console.ReadKey();
      }
   }
}

運行結果:

總面積: 35

二、基類的初始化

派生類繼承了基類的成員變量和成員方法,因此父類對象應在子類對象創建之前被創建。

可以在 成員初始化列表 中進行父類的初始化。

例:

using System;
namespace RectangleApplication
{
   class Rectangle
   {
      // 成員變量
      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("長度: {0}", length);
         Console.WriteLine("寬度: {0}", width);
         Console.WriteLine("面積: {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("成本: {0}", GetCost());
      }
   }
   class ExecuteRectangle
   {
      static void Main(string[] args)
      {
         Tabletop t = new Tabletop(4.5, 7.5);
         t.Display();
         Console.ReadLine();
      }
   }
}

運行結果:

長度: 4.5
寬度: 7.5
面積: 33.75
成本: 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;
   }

   // 基類 PaintCost
   public interface PaintCost // interface 表示 C# 的接口
   {
      int getCost(int area);

   }
   // 派生類
   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();
         // 打印對象的面積
         Console.WriteLine("總面積: {0}",  Rect.getArea());
         Console.WriteLine("油漆總成本: ${0}" , Rect.getCost(area));
         Console.ReadKey();
      }
   }
}

運行結果:

總面積: 35
油漆總成本: $2450

總結:繼承接口的意義

既然接口裏面沒有方法的具體實現,那還要接口做什麼用呢?

答案是:爲了規範和協作性。

接口下面定義的方法,依然繼承了這個接口就必須實現,名字必須統一,比如我要計算一個A班級的平均成績,定義一個接口,那繼承以後,必須計算平均成績,而且這個方法的名字還要統一,但是怎麼計算,這個接口不管,由子類自由實現。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章