oop - 为什么维基百科说 "Polymorphism is not the same as method overloading or method overriding."

标签 oop polymorphism overloading overriding

我环顾四周,找不到任何类似的问题。

这是我从 Wikipedia 得到的段落:

多态性与方法重载或方法覆盖不同。多态性仅涉及将特定实现应用于接口(interface)或更通用的基类。方法重载是指在同一个类中具有相同名称但不同签名的方法。方法覆盖是子类替换其父类的一个或多个方法的实现的地方。方法重载和方法覆盖本身都不是多态性的实现。

这里有人可以更清楚地解释它,尤其是“多态性与方法覆盖不同”部分吗?我现在很困惑。提前致谢。

最佳答案

多态性(很简单地说)是在需要基类的地方使用派生类的可能性:

class Base {

}

class Derived extends Base  {

}

Base v = new Derived(); // OK

另一方面,方法覆盖是 Wiki 所说的一种更改派生类中方法行为的方法:
class Shape  {
  void draw() { /* Nothing here, could be abstract*/ }
}

class Square extends Shape  {
  @Override
  void draw() { /* Draw the square here */ }
}

重载与继承无关,它允许定义更多具有相同名称的函数,但它们所采用的参数不同。

关于oop - 为什么维基百科说 "Polymorphism is not the same as method overloading or method overriding.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3343625/

相关文章:

c++ - 维护当前选定的对象是否很好地使用状态模式?

php - 你能得到调用类的变量吗?

oop - 理解 Dart 中的 'implements' 和 'with'

c++ - C++ 中的多重多态

c++ - 静态数组初始化可以是 'overloaded' 吗?

c++ - const 和通用引用的重载是否必要?

Java方法重载和调用

c++ - 面向对象的设计选择

c++ - 运算符的不明确重载