1. 程式人生 > >多態是面向接口編程的概念

多態是面向接口編程的概念

span specified 繼承 com phi hoc 屬於 per 多態

對象指針本身也是接口中的一個參量。

然而,在更廣闊的計算機領域中,多態這個詞並不只是局限在類繼承和虛函數上。只要是同名函數能夠根據參數的類型不同而獲得不同的實現,我們都可以叫它“多態”。

多態可以分成三類,Ad hoc 多態(Ad hoc ~),參數化多態(Parametric ~),子類型多態(Subtype ~)。

對於Ad hoc多態,首先要神會這個詞,我個人通常將其簡單替換成 ”The fucking“,“那個多態”。C++中的重載,例子中的函數 f3() 就屬於此類。多個同名函數根據參數的不同,選擇不同的實現。第二種的典型例子,就是例子中的f1(),通過參數化類型來完成多態。第三種,局限於多態化的函數參數類型是Subtyping的關系,它是C++上狹義的多態。

參考:

Polymorphism (computer science)

https://zhuanlan.zhihu.com/p/20413538

  • Ad hoc polymorphism: when a function denotes different and potentially heterogeneous implementations depending on a limited range of individually specified types and combinations. Ad hoc polymorphism is supported in many languages using function overloading.
  • Parametric polymorphism: when code is written without mention of any specific type and thus can be used transparently with any number of new types. In the object-oriented programming community, this is often known as generics or generic programming. In the functional programming community, this is often shortened to polymorphism
    .
  • Subtyping (also called subtype polymorphism or inclusion polymorphism): when a name denotes instances of many different classes related by some common superclass.[3]

多態是面向接口編程的概念