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条回答
forever°为你锁心
2楼-- · 2019-01-04 16:23

Multiple forms of a single object is called Polymorphism.

查看更多
迷人小祖宗
3楼-- · 2019-01-04 16:24

Fruit can be eaten, as a general rule, but different types of fruit is eaten in different ways. An apple, which is a fruit, can be eaten (because it is a fruit). A banana can also be eaten (because it is also a fruit), but in a different manner from an apple. You peel it first.

Well, at least I do, but I'm weird in some manners so what do I know.

This illustrates inheritance (fruit can be eaten), polymorphism (something that eats fruit can eat all types of fruit), and encapsulation (a banana has a skin).

Seriously though, object inheritance, polymorphism, encapsulation, virtual things, abstract things, private things, public things, these are all hard concepts. If someone absolutely wants to have a 2-sentence definition of this then please tag the question as a code-golf variant, because two such sentences will have to be so terse that unless you know what it is already you won't learn enough about it to know what you need to learn more about.

查看更多
淡お忘
4楼-- · 2019-01-04 16:24

Giving a single name to a set of analogous operations on different types. When done well, the analogy is obvious e.g. "adding" numbers arithmetically and "adding" strings by concatenation (which sums their lengths).

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

Polymorphism allows the expression of some sort of contract, with potentially many types implementing that contract (whether through class inheritance or not) in different ways, each according to their own purpose. Code using that contract should not(*) have to care about which implementation is involved, only that the contract will be obeyed.

(*) In the ideal case, anyway - obviously quite often the calling code has chosen the appropriate implementation very deliberately!

查看更多
时光不老,我们不散
6楼-- · 2019-01-04 16:25

I guess sometimes objects are dynamically called. You are not sure whether the object would be a triangle, square etc in a classic shape poly. example.

So, to leave all such things behind, we just call the function of derived class and assume the one of the dynamic class will be called.

You wouldn't care if its a sqaure, triangle or rectangle. You just care about the area. Hence the getArea method will be called depending upon the dynamic object passed.

查看更多
小情绪 Triste *
7楼-- · 2019-01-04 16:29

polymorphism == multiple classes + same method signatures + class-specific behavior.

查看更多
登录 后发表回答