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

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

问题描述

我查看了其他定义和解释,但没有一个令我满意.我想看看是否有人可以在不使用任何代码或示例的情况下最多用两句话定义多态性.我不想听到所以你有一个人/汽车/开罐器......"或者这个词是如何派生的(没有人对你知道 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天全站免登陆