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

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

 

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

多态可以分成三类,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]

转载于:https://www.cnblogs.com/feng9exe/p/8303815.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值