“多态性与方法重载或方法覆盖不同。”
“多态性与方法重载或方法重写不同。……两者都不是……本身就是多态性的实现”。
这是来自 wikipedia 的引用
,但是 Timothy Budd 在《面向对象编程》一书中指出有“四种不同形式的多态性”:
重载(临时多态性)
重写(包含多态性)
多态性变量(赋值多态)
泛型
谁是对的?谢谢
"Polymorphism is not the same as method overloading or method overriding. ... Neither ... are by themselves implementations of polymorphism".
This is a quote from wikipedia
However in the book "Object-Oriented Programming" Timothy Budd states there are "four different forms of polymorphism":
overloading (ad hoc polymorphism)
overriding (inclusion polymorphism)
polymorphic variable (assignment polymorphism)
generics
Who is right? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
多态性是编程语言的一个特性或特征。编程语言要么支持它,要么不支持。由于编程语言有时属于本质上不同的范式的保护范围,因此不同的范式(函数式编程或面向对象编程)可能对多态性在该特定范式中如何表达的解释和应用略有不同。
据我所知,在OOP中,多态被认为是基本原则之一,也是一个非常独特的原则。大多数面向对象语言的许多特性都具有多态性。简而言之,当具有多态实现的对象的调用者不知道该对象的确切类型时,多态性最明显。它通常是继承和转换的结果,也称为子类型多态性,并通过使用vTable。
我(以及许多作者)都认为运算符重载是多态性的一种表现。因此,如果您重载 == 运算符以获取 TypeA == TypeB,那么如果您正在比较包含类型 A 或 B 的随机元素的列表中的元素,那么您实际上将 TypeB 解释为 TypeA,您并不真正关心传入的内容,因为他们都可以受到平等对待。与许多其他辩论一样,这一辩论也有支持者和反对者。
但这就是面向对象编程的故事的结局。
在函数(声明性)语言(Lisp、F#)中,由于函数(与对象)是第一等公民,多态性是通过函数之间的关系来表达的,并且表现得有点不同。请参阅类型多态性
我想说的最后一句话是,我热爱维基百科,就像热爱维基百科一样。其余的,但你必须始终对文章持保留态度,在没有确认其他来源的情况下切勿盲目相信它们。如果您想了解 OOP 的真正原理,您应该从这里开始:
Object -面向软件构建(Bertrand Meyer)
Polymorphism is a characteristic or feature of programming languages. Programming languages either support it or they don’t. Since programming languages fall under the umbrella of sometimes substantially different paradigms, different paradigms (functional programming or object oriented programming) may have slightly different interpretations and applications of HOW polymorphism is expressed in that particular paradigm.
As far as I know, in OOP polymorphism is considered one of the basic principles and a very distinctive one. Most of the object oriented languages have polymorphism among its many features. In a nutshell, polymorphism is best seen when the caller of an object with polymorphic implementation is not aware of the exact type the object is. Is is often a consequence of inheritance and casting, is also called subtype polymorphism, and works through the use of vTables.
I share the idea (along with many authors) that operator overload is a manifestation of polymorphism. So if you overload the == operator to take TypeA == TypeB, you are effectively interpreting TypeB as a TypeA if you are comparing elements in a list containing random elements of types A or B, you don't really care what comes in, since they can all be treated for equality. Like many other debates this one has defenders and haters.
But that's the end of the story for OOP.
In functional (declarative) languages (Lisp, F#) since the first class citizens are functions (vs Objects) polymorphism is expressed through relationships between functions and is manifested a bit differently. See Type Polymorphism
The last word I want to put out there is that I love Wikipedia as much as the rest, but you must always take articles with a grain of salt and never trust them blindly without confirming other sources. If you want to get the truth about the true principles of OOP, you should start here:
Object-Oriented Software Construction (Bertrand Meyer)
我相信我所见过的关于多态性的最佳定义之一是指在运行时识别对象的类型。这似乎强调对象的运行时类型可能与其声明的类型不同,并且在对象上调用的方法将在运行时过程中匹配。
I believe one of the best definitions I've seen about polymorphism refers to an object's type being discerned at Runtime. This seems to emphasize that the objects Runtime type may differ from it's declared type and that the methods invoked on the object will be matched during the Runtime process.
我将把确切的定义留给更了解的人(从完全纯粹主义的角度来看),但从纯粹语义的角度来看,这些陈述不一定相互矛盾。
一种是列出某事物的“四种不同形式”,另一种则表示这四种形式中的两种“本身”并不构成该事物。有人可能会说,“四种形式”的作者不一定说每一种形式本身都是整体的“完整”例子,而是说它们是整体的组成部分。
也就是说,我认为“四种形式”的作者更正确,而维基百科的作者可能只是试图解析一些东西。和你一样。 :p
I will leave the exact definitions to someone who knows better (from a complete purist point of view), but from a purely semantic point of view, those statements do not necessarily contradict each other.
One is listing 'four different forms' of something, and the other says that two of those four forms do not "by themselves" comprise the thing. One could argue that the 'four forms' writer isn't necessarily saying that each of those forms is, itself, a "complete" example of the whole, but that they are components of.
That said, I think that the 'four forms' writer is more correct, and the wikipedia writer is perhaps just trying to parse something a bit much. As are you. :p