Compile time polymorphism / Overloading
Why is overload considered a form of polymorphism, or a way to achieve polymorphism.
Polymorph开发者_如何学Goism for me mean multiple form and I don't quite see how overloading help achieving this.
Polymorphism is something that happens at runtime. In terms of OOP, it's the practice of calling a method, and, at compile time, not knowing which object the method will be called on. The classic example is with Shapes, Circles, Squares, etc. You have a collection of Shapes, each of which is instantiated to a Circle, Square, whatever. The compiler neither knows nor cares which type of Shape you're calling Draw
or Erase
on at any given time; that determination happens when the program is run.
Method overloading happens at compile time. You have multiple methods with the same name, but different parameters. When you call one of the methods, the compiler will decide which of the overloads best matches based on the parameters you supplied.
Why is overload considered a form of polymorphism, or a way to achieve polymorphism.
At root, polymorphism is the ability to treat multiple types under the same API. So technically, method overloading does fall under this. In fact, according to wikipedia, method overloading is known as "ad-hoc polymorphism", but I've never heard this term used in real life (not that I'm the final authority). Usually polymorphism refers exclusively to the dynamic binding of methods, as in the Shape, Circle, Square example.
There's also "parametric polymorphism", though that's usually, again by my perhaps limited experience, referred to as either generics or templates depending on the language (they are not the same; there are subtle differences between the two)
Why is overload considered a form of polymorphism
It is not. This is incorrect information. Overload is not polymorphism. From Wikipedia's Polymorphism in object-oriented programming page:
Neither method overloading nor method overriding are by themselves implementations of polymorphism.
Method overloading might be a form of polymorphism in a dynamic language or in a language that uses multiple dispatch to resolve method overloading, but it is not in C#. There is no polymorphism in C#'s method overloading, because the overload for each call is chosen strictly at compile-time (except in the case of dynamic
variables).
UPDATE
@Lee states that "There's no requirement for polymorphism to be a run-time mechanism."
Yes, there is.
In C#, method overloading is a handy way to communicate intent, and avoid obnoxious method names, by giving multiple methods the same name. But suppose for a second that C# didn't have method overloading. Suppose that a method's signature consisted entirely of its name, ignoring parameter types. What would be lost in this hypothetical language?
Well, in this language we would have to give every method a different name. We would have to create WriteLineString, WriteLineInt32, WriteLineUInt32, etc. It would be pretty terrible from a useability perspective.
But what would change in the expressive power of the language? Nothing. We could still write the same code with just a few changes of the method names. The compiler would resolve the same calls to the same places. The IL would be exactly the same. There would be no difference in efficiency or power or complexity.
So if expressive power of the language, and the compiled code, would be the same in C# and in no-overloads-C#, then how could overloads possibly be considered "polymorphism"? Polymorphism means exhibiting different behaviors based on an object's type. If removing overloading does nothing to change an object's behavior, then how could overloading be involved in polymorphism?
Run time vs. compile time behavior is at the very heart of polymorphism, and it has nothing to do with method overloading.
It's debatable, but some people consider it a form beacause it allows you to handle multiple forms while using the same name. Such as:
public static class Math
{
int Min(int x, int y) { ... }
double Min(double x, double y) { ... }
}
In this way, you can call Math.Min and it will choose the right method based on the type of argument passed to it (yes, you could do that with generics too, but just as a simple illustration of overloading).
You use method overloading when you need different versions of the same method that take different types of parameters. Overloading is an object-oriented programming concept, as is polymorphism. Overloading is not the same as object polymorphism.
With object polymorphism you define different behavior for objects that provide an implementation of the same method and signature.
精彩评论