多态性 - 重载/覆盖
我知道这个问题已经在 StackOverflow 上被解决了,而且已经有很多关于这个问题的问题了。 我可能已经阅读了其中的每一本,但仍然存在一个棘手的疑问:我认为我非常了解重载和重写。让我着迷的是多态性。
例如,这个问题的公认答案用 shape.Draw().我很困惑这与覆盖有何不同(其他时候我很困惑它与重载有何不同)。
另外 - 多态本质上意味着从抽象类派生吗? (我认为我读过的关于该主题的几乎所有答案都使用抽象动物类,并使猫和狗喵喵/吠叫:)
总结,我的问题是:
什么多态性是关于重载和重写吗?
有人可以解释一下没有抽象类的多态性吗 - 谢谢!
重载/重写不是多态性的子类型,不是吗?
编辑添加第三个问题并修改第二个问题。
I know that this question has been done to death at StackOverflow and that there are numerous questions posted on this already. I've probably read every one of them and yet, there's this niggling doubt: I think I understand Overloading pretty well, and Overriding. What gets me is Polymorphism.
For example, the accepted answer to this question explains this with shape.Draw()
. I'm confused as to how this is different from Overriding (other times I'm confused with how it is different from Overloading).
Also - does Polymorphism inherently mean deriving from an abstract class? (I think almost all the answers I've read on the topic uses an abstract animal class and makes a cat and a dog meow/bark :)
To sum up, my questions are:
What is Polymorphism w.r.t. Overloading and Overriding?
Could somebody please explain Polymorphism without an abstract class - thanks!
Overloading/Overriding are not subtypes of Polymorphism, are they?
Edited to add a 3rd question and modify the 2nd question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
回答您的问题:
这是一个不涉及抽象类的多态性示例。
C
类中出现多态性是因为可以通过对 A 对象的引用或对 B 对象的引用来调用SomeStatic
方法>。如果通过对 A 的引用来调用它,则将调用A
的a
方法。如果通过引用 B 来调用它,则将调用B
的a
方法。这种在运行时改变被调用的实际方法的能力称为多态性。重载与多态性几乎没有任何关系。事实上,如果您愿意,您可以进行重载而不涉及继承。您甚至可以进行重载而不涉及面向对象。 重载只是让两个函数具有相同的名称但具有不同的参数。
另一方面,重写只是在专门(继承)类上重新定义方法。 重写方法对于多态性的发生是必要的。否则,运行时将没有双重可能性(仔细查看示例)。
C类是理解这一切的关键:
Poly:来自希腊语。意思是许多。
Morph:来自希腊语。意思是形式。
因此,在多态中,有“许多”(多)“形式”(变形 >) 调用方法。将调用哪一个取决于用于调用该方法的对象。
To answer your questions:
Here's an example with polymorphism happening with no abstract classes involved.
Polymorphism in class
C
occurs becauseSomeStatic
method could be call with a Reference to A object or a Reference to B object. If it's called with a reference to A,A
'sa
method will be called. If it's called with a reference to B,B
'sa
method will be called. This ability of changing, on runtime, the actual method being called is called Polymorphism.Overloading barely has anything to do with Polymorphism. In fact, you can hace overloading with no inheritance involved if you want. You could even have overloading with no object orientation involved. Overloading is just letting two function to exist with the same name but with different parameters.
Overriding on the other hand, is just re-defining a method on a specialized (inherited) class. Overriding a method is necessary for polymorphism to happen. Otherwise, the would be no DUAL POSSIBILITIES on runtime (take a close look at the example).
Class C is the key to understand it all:
Poly: comes from greek. Means many.
Morph: comes from greek. Means form.
So in Polymorphism there are "many" (poly) "forms" (morph) of calling a method. Which one will be called, depends on the object being used to call the method.
实际上,除了重载和重写之外,多态性并不是单独的东西。
重载和重写都是一种特定类型的多态性:
Actually polymorpishm is not something separate beside of overloading and overriding.
Both - overloading and overriding - are a specific type of polymorphism:
多态性是一个 OOP 概念,其中单个任务可以通过多种方式执行。例如。您可以通过借记卡、网上银行支付账单。
它有两种类型:
1)编译时多态性:这会导致静态绑定(编译器用来匹配签名的签名匹配技术)。运算符重载、函数重载实际上是实现编译时多态性的一种方式。
2) 运行时多态性:导致动态绑定。重写(在后续子类中重新定义方法)是实现运行时多态性的一种方法。
Polymorphism is an OOP concept in which a single task is performed in many ways. Eg. You can pay bill through debit card , through netbanking.
It is of two type :
1) Compile time polymorphism : which causes static binding(A signature matching technique that compiler use to match the signature). Operator overloading , function overloading is actually a way to implement compile time polymorphism.
2) Run time polymorphism : which causes dynamic binding. Overriding(redefining a method in subsequent sub classes) is a way of implementing the run time polymorphism.