多态(Polymorphism)是OOP的一个最重要和最基本的概念之一。不同的面向对象程序设计语言都有不同的实现多态的方法。最基本的方法就是inheritance加override了,当然还有其它的一些方法。

现在有一个问题:overload是否也是实现多态的方法之一?

Thinking in C++中认为:多态的实现方法只有一个,也就是virtual function。

而也有其它的一些人认为,实现多态的方法很多。你认为呢?

以下来自于国外一个Blog的讨论,也说明overload是否多态是个问题。大家一起讨论讨论。

链接来自于:http://www.scottcreynolds.com/

Polymorphism is the one that always gave me trouble when I was learning OOP because it sounds a hell of a lot more complicated than it is at root, but also because there is just so much to polymorphism once you get deeper and deeper into OOP.  At root, polymorphism means that one thing can have many shapes (or, in World of Warcraft, Polymorph is where a mage turns you into a sheep).  From Wikipedia:
polymorphism is behavior that varies depending on the class in which the behavior is invoked. For example, the result of bark() for a Dog would differ from the result of bark() for a Jackal; and in a more sophisticated animal-emulation program, bark() would differ for a Chihuahua and a Saint Bernard.

This definition is a little incomplete in my opinion.  I am going to borrow a line from Raymond Lewallen (who did a great series on the 4 principles of OOP a while back) where he says that while polymorphism can be coupled with inheritance (as is implied by the wikipedia definition above) it does not require inheritance.  You can see polymorphism at work in one class in the form of overloading.  You can see polymorphism at work when implementing interfaces (covered later) that allows you to treat one object type like another if they share common interfaces.  You can see polymorphism at work in inheritance via overriding inherited behaviors.  Generics available in C# 2.0 are a type of polymorphism at work (called parametric polymorphism).  A lot of that is way beyond the scope here.  I only want to discuss overloading and overriding here, because those are the least complicated and the most often encountered when you first begin OOP and this is just an overview article.  Polymorphism is one of those things that you can talk about for days and still not see the end of.  Because of the many places you can see it at work, it’s one of the hardest parts of OO to define, and to fully understand, until you see it from all its different sides.

Overloading is a type of polymorphism (sometimes called ad-hoc polymorphism) where you essentially have multiple functions that perform the same task, return the same thing, and have the same name.  They differ in the number and/or type of arguments passed in.  Some will say that method overloading is not polymorphism.  Others will say it is.  I’m not here to deal in academics.  In the real world, overloaded behavior is considered to be polymorphic behavior, at least in the sense that from the perspective of the consumer of the API it’s polymorphic, but maybe in the perspective of the author it’s just syntactic sugar for multiple methods.  Overloading is important to understand and is a key feature of OOP, and I’m putting it under polymorphism.  Deal.  If you know enough about it to argue with me about whether or not it goes here…this post ain’t for you bub. :P