多态性 - 用两句话定义 [英] Polymorphism - Define In Just Two Sentences

查看:17
本文介绍了多态性 - 用两句话定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过其他定义和解释,但没有一个让我满意.我想看看是否有人可以在不使用任何代码或示例的情况下最多在两个句子中定义多态性.我不想听到So you have a person/car/can opener..."或者这个词是如何派生的(没有人会因为你知道 poly 和 morph 的意思而印象深刻).如果您非常了解什么是多态性并且精通英语,那么您应该能够用简短但密集的定义来回答这个问题.如果您的定义准确地定义了多态性,但过于密集以至于需要反复阅读,那么这正是我要寻找的.

为什么只有两句话?因为定义简短而智能.解释很长,包含示例和代码.看这里的解释(那些页面上的答案对我的问题不满意):

多态 vs 覆盖 vs 重载
尽量简单地描述多态

我为什么要问这个问题?因为我被问到了同样的问题,我发现我无法给出一个令人满意的定义(以我的标准来说,这是相当高的).我想看看这个网站上有没有大佬能做到.

如果你真的做不到这两个句子的要求(这是一个很难定义的主题),那么你过去也可以.这个想法是有一个定义,实际上定义了什么是多态,而不解释它的作用或如何使用它(得到区别?).

解决方案

多态允许表达某种契约,潜在的许多类型以不同的方式实现该契约(无论是否通过类继承),每种类型都根据它们的不同自己的目的.代码使用那个契约不应该(*)关心涉及到哪个实现,只关心契约会被遵守.

(*) 在理想情况下,无论如何 - 显然,调用代码通常非常有意地选择了适当的实现!

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?).

解决方案

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!

这篇关于多态性 - 用两句话定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆