与多态相混淆:参数,包含,强制和重载 [英] Confusion with polymorphism: Parametric, Inclusion, Coercion, and Overloading

查看:88
本文介绍了与多态相混淆:参数,包含,强制和重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读stackoverflow问题时,普遍的共识似乎是过载不是多态性的一部分.

Reading stackoverflow questions, the general consensus seems to be that overloading is not part of polymorphism.

但是,我的OOP讲义指出:

However, my OOP lecture notes state that:

多态性有四种:参数多态,包含,强制和重载".

"There are four kinds of polymorphism: Parametric, Inclusion, Coercion, and Overloading".

在注释中,它指的是使用具有不同参数的方法进行重载,还涉及对运算符进行重载,例如+代表整数和浮点数.

In the notes, it refers to overloading with methods with different parameters, and also overloading operators, e.g. + in the sense of ints and floats.

Wikipedia还指出使用函数重载,许多语言都支持Ad hoc多态性."

Wikipedia also states "Ad hoc polymorphism is supported in many languages using function overloading."

因此,我对为什么人们说这不是多态性的一部分感到困惑,正如我认为的那样;我们对一种方法有不同的形式.

Thus i'm confused as to why people say this isn't part of polymorphism, as it seems to be in my opinion; we have different forms for one method.

有人可以详细说明吗?

谢谢.

推荐答案

如果您对Poly-Morphism一词的含义进行了严格的定义,那么是的,重载就是多态.这些方法具有相同的名称,不同的签名,并且运行时根据您使用的签名知道要使用哪种方法.这是同一方法的多种形式.它不是具有类和继承,动物,狗和猫等的多态性的经典"描述.某些语言具有运算符重载.有很多相同类型的表格吗?

If you take a strict definition of what the word Poly-Morphism means, then yes, overloading is polymorphism. The methods have the same name, different signatures and the runtime knows which method to use based upon the signature you use. That's many forms of the same method. It is not "classic" descriptions of polymorphism with classes and inheritance, animals, dogs, and cats, etc. Some languages have operator overloading. Is that many forms of the same type?

这实际上取决于您说的是多态.是的,如果您说许多形式仅与对象有关,那么就不能在OOP意义上将其重载为真实"多态,因为它们是方法,而不是对象.

It really depends on what you say is polymorphing. If you say that many forms only relates to objects, they yeah, you can't have overloading as "real" polymorphism in the OOP sense because they are methods, not objects.

这可以帮助多态vs覆盖vs重载

您会看到很多意见.

这篇关于与多态相混淆:参数,包含,强制和重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆