深入淺出OOP(二): 多態和繼承(繼承)

來源:互聯網
上載者:User

標籤:

本文是深入淺出OOP第二篇,主要說說繼承的話題。

 

繼承的介紹

在OOP中,繼承有如下的定義:

  • 繼承是一種OOP的機制,用於派生繼承預定義的類

  • 在這個繼承關係中,預定義的類是基類,新類是子類

  • 繼承常常用於實現代碼重用

  • 繼承允許子類複用基類非private的的資料和方法

繼承的實現

建立一個Console工程,命名為InheritanceAndPolymorphism。添加ClassA、ClassB類,並拷貝下面的代碼:

  x =

 

在Program.cs中,調用ClassA

  Main(=

如果運行,肯定會報錯的。

Error: ‘InheritanceAndPolymorphism.ClassA‘ does not contain a definition for ‘Display1‘ and no extension method ‘Display1‘ accepting a first argument of type ‘InheritanceAndPolymorphism.ClassA‘ could be found

因為我們在ClassA中未定義Display1的方法。 下面我們重寫,使ClassA繼承自ClassB。

  x =

 

再次運行,結果如下:

ClassB Display1

ClassA已經可以訪問其基類的Display1函數了,這個簡單的執行個體說明了繼承可複用基類的妙處,下面這張圖以父子財產繼承關係說明了繼承的意義

 

再來看另外一個情境,假設ClassA也有一個Display1函數,簽名和其基類一樣的:

   x =

執行後結果如下:

ClassA Display1

看起來結果是對的,ClassA預設調用了自己的Display1函數,但是Visual Studio有一個警告:

Warning: ‘InheritanceAndPolymorphism.ClassA.Display1()‘ hides inherited member ‘InheritanceAndPolymorphism.ClassB.Display1()‘. Use the new keyword if hiding was intended.

C#中對方法的調用首先是查詢ClassA自己中有無Display1函數,再查詢其基類有無Display1函數。在基類和子類出現同樣函數的情況現實項目中是存在的,可能是基類代碼過於陳舊了,子類既想用同簽名的函數,又無法停止基類的同簽名函數,故會出現這樣的警告---儘管邏輯正確,但是這種設計還是有一些瑕疵的。

 

我們再試試在CalssA中通過base調用基類同名方法的情況:

   x =

 

執行結果如下:

ClassA Display1

ClassB Display1

這個實驗說明C#提供了base關鍵詞,用於在繼承中子類調用基類的函數或者變數(非private類型)。

 

 

同樣的,在ClassA.Display1中調用其基類的Display2也是可以的,代碼如下所示:

          x =                          Main(=

 

執行結果如下:

ClassA Display1

ClassB Display2

 

那麼可否通過基類調用其子類的函數呢?

          x =                         Main(=

 

運行報錯:

Error: ‘InheritanceAndPolymorphism.ClassB‘ does not contain a definition for ‘Display2‘ and no extension method ‘Display2‘ accepting a first argument of type ‘InheritanceAndPolymorphism.ClassB‘ could be found

原因是繼承無法實現逆向調用,既基類無法調用子類。

除了建構函式和解構函式,子類繼承了其基類的一些(包括private的成員變數和成員函數,只是無法訪問)。

在C#中,一個類預設繼承的是object類型,object是C#所有參考型別的基類;同時,繼承具有傳遞性,如ClassC繼承自ClassB,ClassB繼承自ClassA,則ClassC可完全複用ClassA的資料和函數---ClassC繼承了ClassA。

C#中所有的類型都可被繼承嗎?

public class ClassW : System.ValueType   {   }   public class ClassX : System.Enum   {   }   public class ClassY : System.Delegate   {   }   public class ClassZ : System.Array   {   }
執行結果:

‘InheritanceAndPolymorphism.ClassW‘ cannot derive from special class ‘System.ValueType‘

‘InheritanceAndPolymorphism.ClassX‘ cannot derive from special class ‘System.Enum‘

‘InheritanceAndPolymorphism.ClassY‘ cannot derive from special class ‘System.Delegate‘

‘InheritanceAndPolymorphism.ClassZ‘ cannot derive from special class ‘System.Array‘

 

啟動並執行結果讓人抓狂

在C#中,自訂類無法繼承自C#內建的一些類,如System.ValueType, System.Enum, System.Delegate, System.Array, etc。

 

下面這個例子我們再看看C++中的多類繼承是否可在C#中實現:

    public class ClassW    {    }    public class ClassX    {    }    public class ClassY : ClassW, ClassX    {    }

執行結果:

Compile time Error: Class ‘InheritanceAndPolymorphism.ClassY‘ cannot have multiple base classes: ‘InheritanceAndPolymorphism.ClassW‘ and ‘ClassX‘.

 

執行結論是:C#僅支援單類繼承,不支援C++的這種星型繼承關係。 要使用星型繼承關係,請用介面實現。

 

那麼可否實現循環相依性繼承呢?

   public class ClassW: ClassY    {    }    public class ClassX: ClassW    {    }    public class ClassY :  ClassX    {    }

 

代碼邏輯很簡單,ClassW繼承自ClassY,ClassX繼承自ClassW, ClassY繼承自ClassX。

但是編譯後報錯了:

Error: Circular base class dependency involving ‘InheritanceAndPolymorphism.ClassX‘ and ‘InheritanceAndPolymorphism.ClassW‘.

 

我們得出一個結論,C#中不許環形依賴繼承。

 

執行個體對象的是否可賦值
   b =    a =

 

Program.cs 代碼如下

              Main(= = ==

 

我們嘗試判斷ClassA、ClassB的對象是否可賦值。

編譯的結果是:報錯了

Cannot implicitly convert type ‘InheritanceAndPolymorphism.ClassB‘ to ‘InheritanceAndPolymorphism.ClassA‘ Cannot implicitly convert type ‘InheritanceAndPolymorphism.ClassA‘ to ‘InheritanceAndPolymorphism.ClassB‘

儘管ClassA和ClassB裡面的資料成員變數a資料一致,都為100,但是這裡用等號比較的是類型--引用地址,故無法進行賦值。

 

我們再來試試繼承關係的:

   b =    a =               Main(= = ==

 

ClassA繼承自ClassB,我們希望可以直接賦值其執行個體對象。

 

運行結果如下:

Error: Cannot implicitly convert type ‘InheritanceAndPolymorphism.ClassB‘ to ‘InheritanceAndPolymorphism.ClassA‘.

運行結論:C#中子類對象可直接賦值給基類對象,基類對象需要往下強轉。代碼修改如下:

   b =    a =               Main(= = ==

 

這樣編譯就通過了。

如果ClassA不繼承自ClassB,則這種強轉在C#中是會報錯的:

Cannot convert type ‘InheritanceAndPolymorphism.ClassA‘ to ‘InheritanceAndPolymorphism.ClassB‘

Cannot convert type ‘InheritanceAndPolymorphism.ClassB‘ to ‘InheritanceAndPolymorphism.ClassA‘

 

 

本節結論
  • 無法阻止子類覆蓋基類同簽名方法

  • 繼承關係是子類的同簽名方法先尋找,再尋找其基類的

  • base關鍵字被C#用於在子類中調用基類函數、變數

  • 繼承關係無法復原轉

  • 除了建構函式、解構函式,子類繼承了基類的一些

  • 自訂類預設繼承自Object類型,但是C#的這些類型不能被繼承:System.ValueType, System.Enum, System.Delegate, System.Array, etc.

  • C#不支援從多類繼承

  • C#不支援迴圈繼承

  • 子類對象可直接賦值給基類,反之需要強轉

 

原文地址:Diving in OOP (Day 2): Polymorphism and Inheritance (Inheritance)

深入淺出OOP(二): 多態和繼承(繼承)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.