问题描述
我对多态、重载和覆盖的概念非常困惑,因为在我看来它是一样的.请解释这些概念,以及它们之间的区别
I am very confused about the concepts of polymorphism ,overloading and overriding because it seems same to me. Please explain these concepts, and how are they different from each other
很困惑所以请正确指导我.
Very confused so please guide me properly.
谢谢
推荐答案
多态可以可以通过覆盖来实现.简而言之,多态性是指对象根据其自身的性质提供不同行为(使用不同实现)的能力.具体来说,取决于它在类层次结构中的位置.
Polymorphism can be achieved through overriding. Put in short words, polymorphism refers to the ability of an object to provide different behaviors (use different implementations) depending on its own nature. Specifically, depending on its position in the class hierarchy.
方法覆盖是指在超类或接口中定义的方法被一个人重新定义其子类,从而修改/替换超类提供的行为.调用一个实现或另一个实现的决定是在运行时动态做出的,这取决于调用操作的对象.注意方法的签名在重写时保持不变.
Method Overriding is when a method defined in a superclass or interface is re-defined by one of its subclasses, thus modifying/replacing the behavior the superclass provides. The decision to call an implementation or another is dynamically taken at runtime, depending on the object the operation is called from. Notice the signature of the method remains the same when overriding.
方法重载与多态无关.它是指定义不同形式的方法(通常通过接收不同的参数编号或类型).它可以看作是静态多态性.调用一个实现或另一个实现的决定是在编码时做出的.请注意,在这种情况下,方法的签名必须更改.
Method Overloading is unrelated to polymorphism. It refers to defining different forms of a method (usually by receiving different parameter number or types). It can be seen as static polymorphism. The decision to call an implementation or another is taken at coding time. Notice in this case the signature of the method must change.
运算符重载是一个不同的概念,与多态有关,它指的是某些依赖于语言的运算符会根据其操作数的类型表现出不同的行为(例如,+
可能意味着与 String
的连接以及与数字操作数的加法).
Operator overloading is a different concept, related to polymorphism, which refers to the ability of a certain language-dependant operator to behave differently based on the type of its operands (for instance, +
could mean concatenation with String
s and addition with numeric operands).
维基百科中的示例非常具有说明性.
The example in Wikipedia is quite illustrative.
以下相关问题也可能有用:
The following related questions might be also useful:
这篇关于多态性、重载和覆盖是相似的概念吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!