1 / 17

Polymorphism

Polymorphism. 多型的功能. 多型提供了「介面與實作分離」的一個重要性,能將 what (是什麼)自 how (怎麼做)之中抽離。 多型( polymorphism )除去型別之間的耦合關係。. 向上轉型( Upcasting ). 「將某個 object reference 視為一個 reference to base type 」的動作,稱為「向上轉型( upcasting )」。. 執行結果:. Method-call 繫結方式. 所謂「繫結( binding )」,就是建立 method call 和 method body 的關聯。

dcalvin
Télécharger la présentation

Polymorphism

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Polymorphism

  2. 多型的功能 • 多型提供了「介面與實作分離」的一個重要性,能將 what(是什麼)自 how(怎麼做)之中抽離。 • 多型(polymorphism)除去型別之間的耦合關係。

  3. 向上轉型(Upcasting) • 「將某個 object reference 視為一個reference to base type」的動作,稱為「向上轉型(upcasting)」。

  4. 執行結果:

  5. Method-call 繫結方式 • 所謂「繫結(binding)」,就是建立method call 和 method body 的關聯。 • Java 函式透過後期繫結達到多型性。 • 後期繫結(late binding):繫結動作在執行期根據 object 的型別而進行。後期繫結也被稱為執行期繫結( run-time binding)或動態繫結(dynamic binding)。 • 後期繫結,必須具備「得以在執行期判知 object 型別」並「呼叫其相應之 methods」的機制。

  6. final Method Call • Java 的所有 methods,除了被宣告為 final者,皆使用後期繫結。 • 為什麼會想將某個 method 宣告為 final呢? • 防止其他人覆寫該 method。 • 「關閉」動態繫結,告訴編譯器:動態繫結是不需要的。

  7. Abstract Classes 和 Abstract Methods • 所謂 abstract class 是一種本身不能被具現化 (instantiated) 的 class,但允許為其內宣告的函式提供實作碼。 • 如果你打算宣告一個 class 卻只提供部分實作,abstract class 就可以一顯身手。

  8. Abstract Classes 和 Abstract Methods (cont.) • abstract class 中未曾實作的函式,稱為 abstract methods。 • 任何一個「擴展 (繼承) abstract class」的 derived class,如果希望被視為 concrete class,必須實作 abstract class 中的所有abstract函式。 • 如果某個 class 擴展 (繼承) 了某個 abstract class,但未能提供所有abstract 函式的實作碼,那麼它依然是個 abstract class。

More Related