多态是面向接口编程的概念

对象指针本身也是接口中的一个参量。

 

然而,在更广阔的计算机领域中,多态这个词并不只是局限在类继承和虚函数上。只要是同名函数能够根据参数的类型不同而获得不同的实现,我们都可以叫它“多态”。

多态可以分成三类,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]
posted @ 2018-01-17 16:53  zzfx  阅读(186)  评论(0编辑  收藏  举报