Polymorphism - Define In Just Two Sentences [close

2019-01-04 15:40发布

I've looked at other definitions and explanations and none of them satisfy me. I want to see if anybody can define polymorphism in at most two sentences without using any code or examples. I don't want to hear 'So you have a person/car/can opener...' or how the word is derived (nobody is impressed that you know what poly and morph means). If you have a very good grasp of what polymorphism is and have a good command of English than you should be able to answer this question in a short, albeit dense, definition. If your definition accurately defines polymorphism but is so dense that it requires a couple of read overs, then that's exactly what I am looking for.

Why only two sentences? Because a definition is short and intelligent. An explanation is long and contains examples and code. Look here for explanations (the answer on those pages are not satisfactory for my question):

Polymorphism vs Overriding vs Overloading
Try to describe polymorphism as easy as you can

Why am I asking this question ? Because I was asked the same question and I found I was unable to come up with a satisfactory definition (by my standards, which are pretty high). I want to see if any of the great minds on this site can do it.

If you really can't make the two sentence requirement (it's a difficult subject to define) then it's fine if you go over. The idea is to have a definition that actually defines what polymorphism is and doesn't explain what it does or how to use it (get the difference?).

30条回答
一纸荒年 Trace。
2楼-- · 2019-01-04 16:30

Polymorphism is language functionality allowing high-level algorithmic code to operate unchanged on multiple types of data. And the other sentence, whatever it was for... ;-P.

( The types C++ supports are listed and contrasted in my answer: Polymorphism in c++ )

查看更多
再贱就再见
3楼-- · 2019-01-04 16:30

Entities of the same type (that is, implemented same interface or derived from same class), behave in different ways (under same method name).

查看更多
手持菜刀,她持情操
4楼-- · 2019-01-04 16:31

Multiple implementations of the same interface.

Example: Many models of telephone implement the numeric keypad interface.

查看更多
ゆ 、 Hurt°
5楼-- · 2019-01-04 16:31

Polymorphism is when different objects respond to the same method in a different way. For example, a car moves on the road while a person walks on the road. Those are two objects responding to the same road in a different way.

查看更多
Fickle 薄情
6楼-- · 2019-01-04 16:32

It seems that the best definitions are provided here, so let me add my two cents please, just for other observers. I hope it could help more.

There are two kinds of polymorphism:

1. Compile-time (static) polymorphism or (ad hoc) polymorphism.

That is simply method overloading and operator overloading

2.  Run time or (dynamic) polymorphism.

The first term is inherited from the Java and C++ terminology.

But in the .NET terminology only the second one (I mean run time polymorphism) is really supposed as polymorphism and simply called polymorphism.

And as far as I know there are three methods for implementing (run time) polymorphism.

 1. Parametric polymorphism or simply the use of generics (templates in C++).

 2. Inheritance-based polymorphism or subtyping.

 3. Interface-based polymorphism.

A simple example Of interface-based polymorphism:

interface Imobile
{
    void Move();
}

class Person :Imobile
{
    public void Move() { Console.WriteLine("I am a person and am moving in my way."); }
}

class Bird :Imobile
{
    public void Move() { Console.WriteLine("I am a bird and am moving in my way."); }
}

class Car :Imobile
{
    public void Move() { Console.WriteLine("I am a car and am moving in my way."); }
}


class Program
{

    static void Main(string[] args)
    {
        // Preparing a list of objects
        List<Imobile> mobileList = new List<Imobile>();

        mobileList.Add(new Person());
        mobileList.Add(new Bird());
        mobileList.Add(new Car());

        foreach (Imobile mobile in mobileList)
        {
            mobile.Move();
        }

        // Keep the console open
        Console.WriteLine("Press any key to exit the program:");
        Console.ReadKey();
    }
}

Output:

 I am a person and am moving in my way.
 I am a bird and am moving in my way.
 I am a car and am moving in my way.
 Press any key to exit the program:
查看更多
放荡不羁爱自由
7楼-- · 2019-01-04 16:32

For a given method signature, different method implementations are run for different, hierarchically related, classes.

查看更多
登录 后发表回答